python - Django Serializer Save models with foreign key relationships -
i trying save model object has foreign key referencing table. trying write serializer same, can't wrap head around how , can't seem find right documentation either. model objects:
class restaurant(models.model): name = models.charfield(null=false, max_length=255) min_order = models.charfield(null=false, max_length=255) # , on class rmenucategory(models.model): category_name = models.charfield(null=false, max_length=255) restaurant = models.foreignkey('restaurant')
my serializer class rmenucategory
model:
class restaurantmenuserializer(serializers.modelserializer): restaurant = serializers.primarykeyrelatedfield() class meta: model = rmenucategory fields = ('id', 'category_name', 'restaurant')
making api call json as:
{ "category_name" : "italian", "restaurant_id" : 4}
this not working when try out following in view:
menu_cat = restaurantmenuserializer(data=data) if menu_cat.is_valid(): category = menu_cat.save() else: exit()
change restaurant_id
restaurant
:
{ "category_name" : "italian", "restaurant" : 4}
Comments
Post a Comment