我的應(yīng)用程序中有各種模型。但是,有一個我無法注冊,所以我可以在admin panel.所以在我的cart應(yīng)用程序admin.py文件中,我可以使用:from django.contrib import adminfrom .models import Cart, CartItem# Register your models here.admin.site.register(Cart)但不是:from django.contrib import adminfrom .models import Cart, CartItem# Register your models here.admin.site.register(Cart, CartItem)因?yàn)槲沂盏酱隋e誤: File "/home/ogonzales/Escritorio/projects_envs/perfectcushion_env/lib/python3.6/site-packages/django/contrib/admin/checks.py", line 26, in check_admin_app errors.extend(site.check(app_configs)) File "/home/ogonzales/Escritorio/projects_envs/perfectcushion_env/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 81, in check if modeladmin.model._meta.app_config in app_configs:AttributeError: 'CartItem' object has no attribute 'model'購物車/models.py:from django.db import modelsfrom shop.models import Product# Create your models here.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_idclass CartItem(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE) cart = models.ForeignKey(Cart, on_delete=models.CASCADE) quantity = models.IntegerField() active = models.BooleanField(default=True) class Meta: db_table = 'CartItem' def sub_total(self): return self.product.price * self.quantity def __str__(self): return self.product
3 回答

拉風(fēng)的咖菲貓
TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個贊
您必須為兩種模型調(diào)用 admin.site.register 兩次:
admin.site.register(Cart)
admin.site.register(CartItem)
添加回答
舉報
0/150
提交
取消