I have a model with many links to it:
class Travel(BaseAbstractModel): tags = models.ManyToManyField( Tag, related_name='travels', ) owner = models.ForeignKey( 'users.TravelUser', related_name='travel_owner' ) payment = models.ForeignKey( Payment, related_name='travels', ) country = models.ForeignKey( Country, related_name='travels, ) ........
Many of these models have only two fields with a unique name and image. I create a serializer for each of these models and put them in the TravelSerializer
class TravelBaseSerializer(DynamicFieldsModelSerializer): owner = UserSerializer(required=False) tags = TagSerializer(many=True) payment = PaymentSerializer() country = CountrySerializer()
Based on the docs, I override create() and update .
The problem is that when I sent JSON data, Django created each model from nested serializers. But I want to create only a copy of Travel . Also I want to receive and respond to a serialized object not only in the pk field.
UPDATE
I solved this problem, put the code in response. Now I can receive and respond with Serializer data without creating an object. But I think DRF provides a more elegant approach than me. This is my first DRF project, maybe I missed something and there is an easier solution.
django django-rest-framework
Ivan Semochkin
source share