无法解析关键字'可用'进入田野。选择是:id

时间:2017-04-05 09:20:59

标签: python django

以下是我的观点的相关部分

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})

这是我的模型的相关部分我认为问题将来自Here

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

以下是我的追溯错误的相关部分

    FieldError at /
Cannot resolve keyword 'available' into field. Choices are: id

1 个答案:

答案 0 :(得分:0)

您是否为模型进行了迁移?如果是,您是否迁移了迁移?

python manage.py makemigrations app_name

python manage.py migrate app_name