我正在尝试访问 Django Admin 中的表格内联中的外键字段。
尽管我尽了最大的努力,但我似乎无法让它发挥作用。我目前的代码是:
class RankingInline(admin.TabularInline):
model = BestBuy.products.through
fields = ('product', 'account_type', 'rank')
readonly_fields = ('product', 'rank')
ordering = ('rank',)
extra = 0
def account_type(self, obj):
return obj.products.account_type
结果是:
'RankingInline.fields' refers to field 'account_type' that is missing from the form.
我也尝试过使用 model__field 方法,我将其用作:
fields = ('product', 'product__account_type', 'rank')
结果是:
'RankingInline.fields' refers to field 'product__account_type' that is missing from the form.
模型定义如下:
class Product(BaseModel):
account_type = models.CharField(choices=ACCOUNT_TYPE_OPTIONS, verbose_name='Account Type', max_length=1, default='P')
class Ranking(models.Model):
product = models.ForeignKey(Product)
bestbuy = models.ForeignKey(BestBuy)
rank = models.IntegerField(null=True, blank = True)
class BestBuy(BaseModel):
products = models.ManyToManyField(Product, through='Ranking')
class BaseModel(models.Model):
title = models.CharField(max_length = TODO_LENGTH)
slug = models.CharField(max_length = TODO_LENGTH, help_text = """The slug is a url encoded version of your title and is used to create the web address""")
created_date = models.DateTimeField(auto_now_add = True)
last_updated = models.DateTimeField(auto_now = True)
我究竟做错了什么?