ProgrammingError at /admin/order/order/ 关系“order_order”不存在第 1 行:SELECT COUNT(*) AS “__count” FROM “order_order”

时间:2021-01-31 21:25:49

标签: python python-3.x django django-rest-framework

我正在使用 django rest framework 并且我有一个名为 order 的应用程序。我想改变我的订单逻辑,我改变了订单应用程序的模型。但是当我想在管理面板中打开 order 和 order_item 模型时,出现此错误:

ProgrammingError at /admin/order/order/
relation "order_order" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "order_order"

而且当我运行 migrate 命令时,它不会很好地迁移并且不会在数据库中创建订单和 OrderItem 表。我正在使用 postgres 数据库。 有我的代码:

型号:

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()

2 个答案:

答案 0 :(得分:0)

想对此发表评论,但您是否尝试过使用 python manage.py migrate yourapp --fake

Here is where I got it from

答案 1 :(得分:0)

我正在使用容器平台并在 docker 平台中使用 posgres。在 posgres 容器中,我使用了网络和存储。因为我在存储中有一个正在运行的容器,它是保存的数据,我无法通过删除 pgadmin 中的数据库或重新启动容器或通过迁移更改数据库来更改它们。我移除了我的容器并制作了新的容器。这样问题就解决了。