serialization - django - "Incorrect type. Expected pk value, received str" error -


i django-rest-framework have following models:

basically every ride has 1 final destination , can have multiple middle destinations.

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') 

serializers.py - rideserializer

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

problem - when trying post /rides/ in order add ride - example {destination=la, leaving_time=19:45, num_of_spots=4}

i error "destination":["incorrect type. expected pk value, received str."]}

couple of questions:

  • what error? if have destination foreign key in ride model, mean destination adding has in destinations table?

  • how fix error?

the issue passing name of related destination object serializer, instead of passing pk/id of destination object. django rest framework seeing , complaining, because can't resolve la object.

it sounds may looking a slugrelatedfield, allows identify objects slug (la in case) instead of primary keys.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -