/admin/order/order/关系"order_order"处的ProgrammingError第1行:SELECT COUNT(*)AS"__count"来自"order_order" [英] ProgrammingError at /admin/order/order/ relation "order_order" does not exist LINE 1: SELECT COUNT(*) AS "__count" FROM "order_order"
问题描述
我正在使用 django rest框架
,并且我有一个名为order的应用程序.我想更改订单逻辑,并更改了订单应用程序的模型.但是当我想在管理面板中打开order和order_item模型时,出现此错误:
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"
,而且当我运行migration命令时,它不能很好地迁移,并且不会在数据库中创建order和OrderItem表格.我正在使用postgres db.有我的代码:
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:
型号:
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()
管理员:
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)
序列化器:
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()
推荐答案
我正在使用容器平台,并在Docker平台中使用posgres.在posgres容器中,我使用了网络和存储.因为我在存储中有一个运行中的容器,所以它是保存的数据,我无法通过删除pgadmin中的数据库或重新启动容器或通过迁移来更改数据库来更改它们.我卸下了容器,制作了新的容器.这样就解决了问题.
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.
这篇关于/admin/order/order/关系"order_order"处的ProgrammingError第1行:SELECT COUNT(*)AS"__count"来自"order_order"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!