'prepopulated_fields'的价值指的是' slug&#39 ;,这不是&shop;产品'的一个属性。

时间:2017-04-05 11:41:04

标签: python django

我在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框架并且得到的错误很麻烦

    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文件内容我认为objects.filter

存在问题
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})

1 个答案:

答案 0 :(得分:0)

您已在models.py中定义了两个名为Product的类。第二个,不包含任何字段,隐藏第一个,它是管理员注册中使用的第二个。删除第二个定义。