django-rest-polymorphic icon indicating copy to clipboard operation
django-rest-polymorphic copied to clipboard

Allow None if the field is marked as allow_null = True

Open robertofd1995 opened this issue 4 years ago • 4 comments

Continuing with the example of the documentation

class ProjectPolymorphicSerializer(PolymorphicSerializer):
    model_serializer_mapping = {
        Project: ProjectSerializer,
        ArtProject: ArtProjectSerializer,
        ResearchProject: ResearchProjectSerializer
    }

if we try to use this serializer in another model:

For example:

model.py

class Question(models.Model):
    question = models.CharField(max_length=240)
    project = models.ForeignKey("Project", null=True, blank=True, on_delete=models.PROTECT)

serializer.py

class QuestionSerializer(serializers.ModelSerializer):
    project = ProjectPolymorphicSerializer(allow_null=True)
    class Meta:
        model = Question
        fields = ("pk", "question", "project")

Before this fix, the code will break

robertofd1995 avatar May 24 '20 12:05 robertofd1995

@apirobot please take a look when you can

robertofd1995 avatar May 24 '20 12:05 robertofd1995

Codecov Report

Merging #27 into master will decrease coverage by 0.96%. The diff coverage is 50.00%.

@@            Coverage Diff             @@
##           master      #27      +/-   ##
==========================================
- Coverage   93.10%   92.13%   -0.97%     
==========================================
  Files           1        1              
  Lines          87       89       +2     
==========================================
+ Hits           81       82       +1     
- Misses          6        7       +1     

codecov[bot] avatar May 24 '20 12:05 codecov[bot]

@robertofd1995 Hi. Thanks for the PR. Could you please add some tests?

denisorehovsky avatar May 26 '20 06:05 denisorehovsky

@robertofd1995 Hi. Thanks for the PR. Could you please add some tests?

Hi, I will try to add them as soon as I have some time

robertofd1995 avatar Jun 16 '20 13:06 robertofd1995