django rest framework - DRF - post to ManyToMany field -


in django app:

models.py:

class destination(models.model):     name=models.charfield(max_length=30)  class ride(models.model):     driver = models.foreignkey('auth.user', related_name='rides_as_driver')     destination=models.foreignkey(destination, related_name='rides_as_final_destination')     leaving_time=models.timefield()     num_of_spots=models.integerfield()     passengers=models.manytomanyfield('auth.user', related_name="rides_as_passenger")     mid_destinations=models.manytomanyfield(destination, related_name='rides_as_middle_destination') 

serializer:

class rideserializer(serializers.modelserializer):     driver = serializers.readonlyfield(source='driver.username')      class meta:         model = ride         fields = ('id', 'driver', 'destination', 'leaving_time',                   'num_of_spots', 'passengers', 'mid_destinations')         read_only_fields = ('id', 'driver', 'passengers', 'mid_destinations') 

as can see, mid_destinations manytomany field.

my question is- how post manytomany field?

to regular fields can post json this, android app:

{ "destination" : "la", "num_of_spots" : "3", "leaving_time" : "14:35"} etc.

how post manytomany field?

thanks ahead!

two steps: 1. remove 'mid_destinations' list of read_only_fields 2. send post data {"mid_destinations": ["la", "sf", "sd"], ...}

i assuming using drf 3.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -