【发布时间】:2009-09-22 21:46:31
【问题描述】:
我希望能够显示图像是否与 list_display 视图中的每个产品相关联。 我似乎遇到了麻烦,因为我正在处理一个关联的对象。
models.py
class ImageMain(models.Model):
"""This is the Main Image of the product"""
product = models.ForeignKey(Product)
photo = models.ImageField(upload_to='lcdtvs')
pub_date = models.DateTimeField('date published', auto_now_add=True)
class Product(models.Model):
name = models.CharField(max_length=100)
poll = models.ForeignKey(Poll)
pub_date = models.DateTimeField('date published', auto_now_add=True)
size = models.IntegerField(default=0)
admin.py
def photo_for_product(obj):
images = obj.imagemain_set.all()
return images[0].photo
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', "photo_for_product")
inlines = [DescriptionInline, FeatureInline, AffiliateInline]
def upper_case_name(self, obj):
return ("%s" % (obj.name)).upper()
def photo_for_product(self, obj):
images = self.imagemain_set.all()
return images[0].photo
admin.site.register(ImageMain)
admin.site.register(Product, ProductAdmin)
由于某种原因,upper_case_name() 在列表视图中显示正常。 photo_for_product() 只是一直显示(无)。
我也尝试在 photo_for_product 方法中使用 pdb,但 Django 不喜欢那样。
我也尝试将 callable 放在 ModelAdmin 方法之前,但是这会产生很多错误:
ProductAdmin.list_display[1], 'photo_for_product' is not a callable or an attribute of 'ProductAdmin' or found in the model 'Product'.
【问题讨论】:
标签: django django-admin