尝试在 Django 管理员中列出我的产品时,我收到以下错误消息(见下文)。但是,如您所见,我已经定义了 qr_code。
在 /admin/products/product/ 'ProductAdmin.fieldsets[4][1]['fields']' 中配置不当是指表单中缺少的字段 'qr_code'。
模型.py
class Product(models.Model):
title = models.CharField(max_length=60)
qr_url = models.URLField(blank=True)
qr_image = models.ImageField(
upload_to="public/uploads/",
height_field="qr_image_height",
width_field="qr_image_width",
null=True,
blank=True,
editable=False
)
qr_image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)
qr_image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)
def __unicode__(self):
return self.title
def qr_code(self):
return '' % self.qr_url
qr_code.allow_tags = True
管理员.py
from django.contrib import admin
from models import Product
class ProductAdmin(admin.ModelAdmin):
list_display = ['title']
fieldsets = (
(None, {
'fields': ('title', 'description', 'active')
}),
('QR Code', {
'classes': ('collapse',),
'fields': ('qr_url', 'qr_code')
}),
)
admin.site.register(Product, ProductAdmin)