我在 Django 中收到一个错误,即"prepopulated_fields"的值指的是"slug",这不是"shop"的属性。产品'。我需要对模型进行哪些更改才能更正此问题?
这是我 admin.py 文件内容:
from django.contrib import admin
from .models import Category, Product
class CategoryAdmin(admin.ModelAdmin):
list_display = ['name', 'slug']
prepopulated_fields = {'slug': ('name',)}
admin.site.register(Category, CategoryAdmin)
class ProductAdmin(admin.ModelAdmin):
list_display = ['name', 'slug', 'price', 'stock', 'available', 'created', 'updated']
list_filter = ['available', 'created', 'updated']
list_editable = ['price', 'stock', 'available']
prepopulated_fields = {'slug': ('name',)}
admin.site.register(Product, ProductAdmin)
这是回溯错误,我不知道如何解决我目前正在使用Django一书示例学习Web框架,并且m得到的错误是scarry
ERRORS:
<class 'shop.admin.ProductAdmin'>: (admin.E027) The value of 'prepopulated_fields' refers to 'slug', which is not an attribute of 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E030) The value of 'prepopulated_fields["slug"][0]' refers to 'name', which is not an attribute of 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E108) The value of 'list_display[0]' refers to 'name', which is not a callable, an attribute of 'ProductAdmin', or an attribute or method on 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E108) The value of 'list_display[1]' refers to 'slug', which is not a callable, an attribute of 'ProductAdmin', or an attribute or method on 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E108) The value of 'list_display[2]' refers to 'price', which is not a callable, an attribute of 'ProductAdmin', or an attribute or method on 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E108) The value of 'list_display[3]' refers to 'stock', which is not a callable, an attribute of 'ProductAdmin', or an attribute or method on 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E108) The value of 'list_display[4]' refers to 'available', which is not a callable, an attribute of 'ProductAdmin', or an attribute or method on 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E108) The value of 'list_display[5]' refers to 'created', which is not a callable, an attribute of 'ProductAdmin', or an attribute or method on 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E108) The value of 'list_display[6]' refers to 'updated', which is not a callable, an attribute of 'ProductAdmin', or an attribute or method on 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E116) The value of 'list_filter[0]' refers to 'available', which does not refer to a Field.
<class 'shop.admin.ProductAdmin'>: (admin.E116) The value of 'list_filter[1]' refers to 'created', which does not refer to a Field.
<class 'shop.admin.ProductAdmin'>: (admin.E116) The value of 'list_filter[2]' refers to 'updated', which does not refer to a Field.
<class 'shop.admin.ProductAdmin'>: (admin.E121) The value of 'list_editable[0]' refers to 'price', which is not an attribute of 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E121) The value of 'list_editable[1]' refers to 'stock', which is not an attribute of 'shop.Product'.
<class 'shop.admin.ProductAdmin'>: (admin.E121) The value of 'list_editable[2]' refers to 'available', which is not an attribute of 'shop.Product'.
System check identified 15 issues (0 silenced).
这是我的模型文件
from django.db import models
from django.core.urlresolvers import reverse
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products')
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name',)
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
class Cateogry(models.Model):
def get_absolute_url(self):
return reverse('product_list_by_category',
args=[self.slug])
class Product(models.Model):
def get_absolute_url(self):
return reverse('product_detail',
args=[self.id, self.slug])
这是我 view.py 文件内容,我认为对象有问题。
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request,'list.html',{'category': category,
'categories': categories,'products': products})
您在 models.py 中定义了两个名为 Product 的类。第二个不包含字段,隐藏第一个字段,它是管理员注册中使用的第二个字段。删除第二个定义。