我在访问我的 django 管理模型时遇到问题

时间:2021-03-05 10:32:19

标签: python django

错误显示:

TypeError at /admin/store/order/23/change/
__str__ returned non-string (type NoneType)
Request Method: GET
Request URL:    http://localhost:8000/admin/store/order/23/change/
Django Version: 3.0.5
Exception Type: TypeError
Exception Value:    
__str__ returned non-string (type NoneType)
Exception Location: C:\Users\BHAVIN\Envs\test\lib\site-packages\django\forms\models.py in label_from_instance, line 1219
Python Executable:  C:\Users\BHAVIN\Envs\test\Scripts\python.exe
Python Version: 3.8.0

我的model.py中的代码:

class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False)
    transaction_id = models.CharField(max_length=100, null=True)
    digital = models.BooleanField(default=False,null=True, blank=True)
    
    def __str__(self):
        return str(self.customer)
        
    @property
    def shipping(self):
        shipping = False
        orderitems = self.orderitem_set.all()
        for i in orderitems:
            if i.product.digital == False:
                shipping = True
        return shipping

    @property
    def get_cart_total(self):
        orderitems = self.orderitem_set.all()
        total = sum([item.get_total for item in orderitems])
        return total 

    @property
    def get_cart_items(self):
        orderitems = self.orderitem_set.all()
        total = sum([item.quantity for item in orderitems])
        return total

1 个答案:

答案 0 :(得分:0)

您在 Order 或 Customer 模型中的一个记录在数据库中可能是空的。