django-rest-polymorphic
django-rest-polymorphic copied to clipboard
Allow None if the field is marked as allow_null = True
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
@apirobot please take a look when you can
Codecov Report
Merging #27 into master will decrease coverage by
0.96%
. The diff coverage is50.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
@robertofd1995 Hi. Thanks for the PR. Could you please add some tests?
@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