我使用inspectdb创建模型文件,但是当我想在管理面板中显示它时出现错误:
(1054, "Unknown column 'order_product.id' in 'field list'")
在我的搜索中,我意识到我必须为每个模型添加一个主键,但是当我这样做时没有发生任何事情。
这是我的代码:
# models.py
class OrderProduct(models.Model):
order = models.ForeignKey('Orders', models.DO_NOTHING)
product = models.ForeignKey('Products', models.DO_NOTHING)
attribute = models.ForeignKey(Attributes, models.DO_NOTHING, blank=True, null=True)
color = models.ForeignKey(Colors, models.DO_NOTHING, blank=True, null=True)
price = models.IntegerField(blank=True, null=True)
attribute_price_differ = models.IntegerField(blank=True, null=True)
attribute_price = models.IntegerField(blank=True, null=True)
number = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'order_product'
class Orders(models.Model):
id = models.BigAutoField(primary_key=True)
user = models.ForeignKey('Users', models.DO_NOTHING)
price = models.IntegerField()
weight = models.IntegerField()
coupon = models.ForeignKey(Coupons, models.DO_NOTHING, blank=True, null=True)
status = models.IntegerField()
process_status = models.IntegerField()
code = models.CharField(max_length=255)
hash = models.CharField(max_length=255)
created_at = models.DateTimeField(blank=True, null=True)
updated_at = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'orders'
# admin.py
@admin.register(OrderProduct)
class PostAdmin(admin.ModelAdmin):
pass