Skip to main content
 首页 » 编程设计

python之Django 错误 : NameError name 'db_table' is not defined

2024年08月12日45mfrbuaa

所以当我尝试进行迁移时,我在 cmd 中遇到了这个错误。这是我的代码:

from django.db import models 
from shop.models import Product 
 
class Cart(models.Model): 
    cart_id = models.CharField(max_length=250, blank = True) 
    date_added = models.DateField(auto_now_add = True) 
    class Meta: 
        db_table - 'Cart' 
        ordering = ['date_added'] 
 
    def __str__(self): 
        return self.cart_id 
 
class CartItem(models.Model): 
    product = models.ForeignKey(Product, on_delete = models.CASCADE) 
    cart = models.ForeignKey(Cart, on_delete=models.CASCADE) 
    quantity = models.IntegerField() 
    class Meta: 
        db_table = 'CartItem' 
 
    def sub_total(self): 
        return self.product.price * self.quantity 
 
    def __str__(self): 
        return self.product 

这是我的 models.py 文件。

感谢您提供的任何帮助。

请您参考如下方法:

如果表 Cart 存在,你应该写

db_table = 'Cart' 

代替

db_table - 'Cart'