Answer the question
In order to leave comments, you need to log in
How to write serializer correctly?
Good afternoon!
For example, there are two models:
class ModelA(models.Model):
name = models.CharField("ФИО", max_length=100)
titke = models.CharField("тайтл", max_length=100)
class ModelB(models.Model):
name = models.CharField("Имя", max_length=100)
modela = models.ForeignKey(ModelA)
class ModelASerializater(serializaters.HyperLinkModelSerialzater):
class Meta:
model = ModelA
fields = (...)
class ModelBSerializater(serializers.ModelSerializer):
modela = ModelASerializater
class Meta:
model = ModelB
fields = (...)
Answer the question
In order to leave comments, you need to log in
class ModelBSerializater(serializers.ModelSerializer):
name_a = serializers.ReadOnlyField(source='modela.name')
class Meta:
model = ModelB
fields = (..., name_a)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question