Это мой models.py
class Product(models.Model):
product = models.CharField(max_length=200)
def __str__(self):
return self.product
class CustOrder(models.Model):
CustomerName = models.CharField(max_length=200)
email = models.EmailField(max_length=70,blank=True, null= True, unique= True)
gender = models.CharField(max_length=6, choices=GENDER_CHOICES)
phone = PhoneField(null=False, blank=True, unique=True)
landmark = models.PointField()
#landmark = models.TextField(max_length=400, help_text="Enter the landmark", default='Enter landmark')
houseno = models.IntegerField(default=0)
#product_name = models.CharField(max_length=200, choices=PRODUCT_CHOICES,default='Boneless chicken')
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True,related_name='production')
quantity = models.IntegerField(default=0)
price = models.ForeignKey(Price, on_delete=models.SET_NULL, null=True,related_name='pricetag')
#price = models.DecimalField(max_digits=50, decimal_places=5, default=48.9)
pay_method = models.CharField(max_length=200,choices=PAYMENT_CHOICES, default='RAZOR PAY')
city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True)
area = models.ForeignKey(Area, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.CustomerName
Это мой serializers.py
class CustOrderSerializer(serializers.ModelSerializer):
class Meta:
model = CustOrder
fields = '__all__'
price = serializers.SlugRelatedField(read_only=True, slug_field='price')
product = serializers.SlugRelatedField(read_only=True, slug_field='product')
area = serializers.SlugRelatedField(read_only=True, slug_field='address')
city = serializers.SlugRelatedField(read_only=True, slug_field='city')```
Фактический результат после публикации:
{
"id": 7,
"price": null,
"product": null,
"area": null,
"city": null,
"CustomerName": "tift",
"email": "rkkk@gmail.com",
"gender": "male",
"phone": "(821) 699-7920, press 91",
"landmark": "SRID=4326;POINT (0.04222869873046874 0.00102996826166618)",
"houseno": 5,
"quantity": 8,
"pay_method": "cod"
}
Ожидаемый результат послеобъявление:
{
"id": 7,
"price": 10,
"product": chicken,
"area": indiranagar,
"city": banaglore,
"CustomerName": "tift",
"email": "rkkk@gmail.com",
"gender": "male",
"phone": "(821) 699-7920, press 91",
"landmark": "SRID=4326;POINT (0.04222869873046874 0.00102996826166618)",
"houseno": 5,
"quantity": 8,
"pay_method": "cod"
}