问题
I'm working with django rest framework
and I have a app named order. I wanted to change my order logic and I changed order app's models. but when I want to open order and order_item models in admin panel I have this error:
ProgrammingError at /admin/order/order/
relation "order_order" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "order_order"
and also when i run migrate command it doesn't migrate well and doesn't create the order and OrderItem tabels in the database. I'm using postgres db. there is my code:
models:
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
ref_code = models.CharField(max_length=20, blank=True, null=True)
# products = models.ManyToManyField(OrderItem)
created_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
def __str__(self):
return self.user.full_name
def total_price(self):
total = 0
for order_item in self.products.all():
total += order_item.get_final_price()
return total
class OrderItem(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='products')
ordered = models.BooleanField(default=False)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveSmallIntegerField(default=1)
def __str__(self):
return f"{self.quantity} of {self.product.name}"
def get_total_item_price(self):
return self.quantity * self.product.price
def get_total_discount_item_price(self):
return self.quantity * self.product.discount
def get_amount_saved(self):
return self.get_total_item_price() - self.get_total_discount_item_price()
def get_final_price(self):
if self.product.discount:
return self.get_total_discount_item_price()
return self.get_total_item_price()
admin:
class OrderAdmin(admin.ModelAdmin):
list_display = ['user',
'ordered',
'ordered_date',
'created_date',
'total_price'
]
list_display_links = ['user', ]
list_filter = ['ordered', ]
search_fields = [
'user',
'ref_code'
]
admin.site.register(Order, OrderAdmin)
admin.site.register(OrderItem)
serializers:
class OrderItemSerializer(serializers.ModelSerializer):
product = serializers.SerializerMethodField()
final_price = serializers.SerializerMethodField()
class Meta:
model = OrderItem
fields = (
'id',
'product',
'quantity',
'final_price'
)
def get_product(self, obj):
return ProductSerializer(obj.product).data
def get_final_price(self, obj):
return obj.get_final_price()
class OrderSerializer(serializers.ModelSerializer):
order_items = serializers.SerializerMethodField()
total = serializers.SerializerMethodField()
class Meta:
model = Order
fields = (
'id',
'order_items',
'total',
)
def get_order_items(self, obj):
return OrderItemSerializer(obj.products.all(), many=True).data
def get_total(self, obj):
return obj.total_price()
回答1:
Wanted to comment this, but have you tried using python manage.py migrate yourapp --fake
?
Here is where I got it from
回答2:
I'm using container platform and using posgres in docker platform. in the posgres container I used a network and storage. because i had an runing container in the storage it was the saved data and I couldn't change them by deleting the db in pgadmin or restarting the containers or changing the db by migration. I removed my containers and made new containers. this way problem fixed.
来源:https://stackoverflow.com/questions/65984415/programmingerror-at-admin-order-order-relation-order-order-does-not-exist-li