|
1 | 1 | from datetime import datetime |
2 | 2 |
|
3 | | -from rest_framework import serializers as drf_serilazers |
| 3 | +from rest_framework import serializers as drf_serilazers, fields as drf_fields |
4 | 4 |
|
5 | 5 | from rest_framework_json_api import relations, serializers |
6 | 6 |
|
@@ -318,15 +318,40 @@ class Meta: |
318 | 318 | exclude = ('polymorphic_ctype',) |
319 | 319 |
|
320 | 320 |
|
| 321 | +class CurrentProjectRelatedField(relations.PolymorphicResourceRelatedField): |
| 322 | + def get_attribute(self, instance): |
| 323 | + obj = super(CurrentProjectRelatedField, self).get_attribute(instance) |
| 324 | + |
| 325 | + is_art = ( |
| 326 | + self.field_name == 'current_art_project' and |
| 327 | + isinstance(obj, ArtProject) |
| 328 | + ) |
| 329 | + is_res = ( |
| 330 | + self.field_name == 'current_research_project' and |
| 331 | + isinstance(obj, ResearchProject) |
| 332 | + ) |
| 333 | + |
| 334 | + if is_art or is_res: |
| 335 | + return obj |
| 336 | + |
| 337 | + raise drf_fields.SkipField() |
| 338 | + |
| 339 | + |
321 | 340 | class CompanySerializer(serializers.ModelSerializer): |
322 | 341 | current_project = relations.PolymorphicResourceRelatedField( |
323 | 342 | ProjectSerializer, queryset=Project.objects.all()) |
| 343 | + current_art_project = CurrentProjectRelatedField( |
| 344 | + ProjectSerializer, source='current_project', read_only=True) |
| 345 | + current_research_project = CurrentProjectRelatedField( |
| 346 | + ProjectSerializer, source='current_project', read_only=True) |
324 | 347 | future_projects = relations.PolymorphicResourceRelatedField( |
325 | 348 | ProjectSerializer, queryset=Project.objects.all(), many=True) |
326 | 349 |
|
327 | 350 | included_serializers = { |
328 | 351 | 'current_project': ProjectSerializer, |
329 | 352 | 'future_projects': ProjectSerializer, |
| 353 | + 'current_art_project': ProjectSerializer, |
| 354 | + 'current_research_project': ProjectSerializer |
330 | 355 | } |
331 | 356 |
|
332 | 357 | class Meta: |
|
0 commit comments