Answer the question
In order to leave comments, you need to log in
How to manage Django Rest Framework serializer field?
# models.py:
class SomeModel(models.Model):
name = models.CharField(max_length=100)
can_use_all = models.BooleanField(default=False)
class RelatedModel(models.Model):
parent = models.ForeighKey('SomeModel', related_name='related_obj')
data = models.TextFields()
# serializers.py
class RelatedSerializer(serializers.ModelSerializer):
class Meta:
model = RelatedModel
fields = '__all__'
class SomeSerializer(serializers.ModelSerializer):
related_obj = RelatedSerializer(many=True, read_only=True)
class Meta:
model = SomeModel
fields = '__all__'
Answer the question
In order to leave comments, you need to log in
As it turned out, the easiest and most reliable way is to make a second serializer that would give all objects. and in the view, override the get_serializer method, which would slip the desired serializer
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question