|
1 | 1 | from datetime import datetime |
2 | 2 |
|
3 | | -from rest_framework import fields as drf_fields |
4 | 3 | from rest_framework import serializers as drf_serilazers |
5 | 4 |
|
6 | 5 | from rest_framework_json_api import relations, serializers |
7 | 6 |
|
8 | 7 | from example.models import ( |
9 | | - ArtProject, |
10 | 8 | Author, |
11 | 9 | AuthorBio, |
12 | 10 | AuthorBioMetadata, |
13 | 11 | AuthorType, |
14 | 12 | Blog, |
15 | 13 | Comment, |
16 | | - Company, |
17 | 14 | Entry, |
18 | | - LabResults, |
19 | | - Project, |
20 | 15 | ProjectType, |
21 | 16 | Questionnaire, |
22 | | - ResearchProject, |
23 | 17 | TaggedItem, |
24 | 18 | ) |
25 | 19 |
|
@@ -347,83 +341,6 @@ class BaseProjectSerializer(serializers.ModelSerializer): |
347 | 341 | } |
348 | 342 |
|
349 | 343 |
|
350 | | -class ArtProjectSerializer(BaseProjectSerializer): |
351 | | - class Meta: |
352 | | - model = ArtProject |
353 | | - exclude = ("polymorphic_ctype",) |
354 | | - |
355 | | - |
356 | | -class ResearchProjectSerializer(BaseProjectSerializer): |
357 | | - # testing exclusive related field on inherited polymorphic model |
358 | | - lab_results = relations.ResourceRelatedField(many=True, read_only=True) |
359 | | - |
360 | | - class Meta: |
361 | | - model = ResearchProject |
362 | | - exclude = ("polymorphic_ctype",) |
363 | | - |
364 | | - |
365 | | -class LabResultsSerializer(serializers.ModelSerializer): |
366 | | - included_serializers = {"author": AuthorSerializer} |
367 | | - |
368 | | - class Meta: |
369 | | - model = LabResults |
370 | | - fields = ("date", "measurements", "author") |
371 | | - |
372 | | - |
373 | | -class ProjectSerializer(serializers.PolymorphicModelSerializer): |
374 | | - included_serializers = { |
375 | | - "project_type": ProjectTypeSerializer, |
376 | | - } |
377 | | - polymorphic_serializers = [ArtProjectSerializer, ResearchProjectSerializer] |
378 | | - |
379 | | - class Meta: |
380 | | - model = Project |
381 | | - exclude = ("polymorphic_ctype",) |
382 | | - |
383 | | - |
384 | | -class CurrentProjectRelatedField(relations.PolymorphicResourceRelatedField): |
385 | | - def get_attribute(self, instance): |
386 | | - obj = super().get_attribute(instance) |
387 | | - |
388 | | - is_art = self.field_name == "current_art_project" and isinstance( |
389 | | - obj, ArtProject |
390 | | - ) |
391 | | - is_res = self.field_name == "current_research_project" and isinstance( |
392 | | - obj, ResearchProject |
393 | | - ) |
394 | | - |
395 | | - if is_art or is_res: |
396 | | - return obj |
397 | | - |
398 | | - raise drf_fields.SkipField() |
399 | | - |
400 | | - |
401 | | -class CompanySerializer(serializers.ModelSerializer): |
402 | | - current_project = relations.PolymorphicResourceRelatedField( |
403 | | - ProjectSerializer, queryset=Project.objects.all() |
404 | | - ) |
405 | | - current_art_project = CurrentProjectRelatedField( |
406 | | - ProjectSerializer, source="current_project", read_only=True |
407 | | - ) |
408 | | - current_research_project = CurrentProjectRelatedField( |
409 | | - ProjectSerializer, source="current_project", read_only=True |
410 | | - ) |
411 | | - future_projects = relations.PolymorphicResourceRelatedField( |
412 | | - ProjectSerializer, queryset=Project.objects.all(), many=True |
413 | | - ) |
414 | | - |
415 | | - included_serializers = { |
416 | | - "current_project": ProjectSerializer, |
417 | | - "future_projects": ProjectSerializer, |
418 | | - "current_art_project": ProjectSerializer, |
419 | | - "current_research_project": ProjectSerializer, |
420 | | - } |
421 | | - |
422 | | - class Meta: |
423 | | - model = Company |
424 | | - fields = "__all__" |
425 | | - |
426 | | - |
427 | 344 | class QuestionSerializer(serializers.Serializer): |
428 | 345 | text = serializers.CharField() |
429 | 346 | required = serializers.BooleanField(default=False) |
|
0 commit comments