For example I have the ff:
models:
class Store(models.Model):
name = models.CharField(max_length = 20)
class Product(models.Model):
name = models.CharField(max_length = 20)
class StoreProducts(models.Model):
store = models.ForeignKey(Store)
product = models.ForeignKey(Product)
qty = models.IntegerField()
serializer:
class StoreProductsSerializer(serializers.ModelSerializer):
class Meta:
model = StoreProducts
fields = ('store', 'product', 'qty')
And defined values:
Store (id, name):
1, store_1
2, store_2
Product (id, name):
1, product_1
2, product_2
Now in the views, I want to add into the StoreProducts from post request without querying the ids of Store and Product like this:
data = {
'store': 'store_1',
'product': 'product_1',
'qty': 1
}
serializer = StoreProductsSerializer(data=data)
if serializer.is_valid():
serializer.save()
Is this possible and how? Thanks