i have error in my django project . when i run 'python manage.py makemigrations' command than come error.
ValueError: Cannot serialize: <django.db.models.query_utils.DeferredAttribute object at 0x000001B5A3078940>
models.py
class Order(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
customer = models.ForeignKey(Customer,
on_delete=models.CASCADE, null=True, blank=True)
quntity = models.IntegerField(default=1)
price = models.IntegerField(default=Product.price)
address = models.CharField(max_length=200)
phone = models.CharField(max_length=13)
date = models.DateTimeField(auto_now=True)
print(Product.price)
def __str__(self) -> str:
return self.product.name
admin.py
from django.contrib import admin
from .models import Order
# Register your models here.
class OrderAdmin(admin.ModelAdmin):
models = Order
list_display =['product','customer','quntity','price','address','phone','date']
admin.site.register(Order,OrderAdmin)
CodePudding user response:
You cannot assign a default value to filed with default=Product.price
So change this line
price = models.IntegerField(default=Product.price)
to
price = models.IntegerField(default=0)
and if you want to set the order price from the product price, you can override the save() method
def save(self, *args, **kwargs):
if self.product is not None:
self.price = self.product.price
super().save(*args, **kwargs)
CodePudding user response:
I think that problem in price attr. You can use def save(), not default