Hi

I have 2 models. Practice and Areas. Areas contains a m2m field to Practice.

I am using ModelViewSets.

When I go to submit a practice using the inbuilt API viewer, I correctly 
see a list of areas from which I can choose, and then save correctly.

I am trying to override the save method in my serializer however, and when 
trying to save a practice it complains the practice must have an ID before 
I can save the objects.

Makes sense.

So I tried:

def create(self, validated_data):
  practice = Practice.objects.create(**validated_data)
  areas = Areas.objects.create(practice=practice, p)

Thinking the practice would save and then have the required PK to form the 
M2M relationship. No such luck. "<practice object> needs to have a value 
for field "id" before this many-to-many relationship can be used."

I'm really struggling to understand M2M using the save method and DRF and 
wondered if anyone could please post a simple example.

I am not using a custom through table, this is just using a simple django 
m2m field.

Many thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to