Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess foreign key fields from Admin Tabular Inline
    primarykey
    data
    text
    <p>I'm trying to access a field of a foreign key within a Tabular Inline in the Django Admin.</p> <p>Despite my best efforts I can't seem to get it working. My current code is:</p> <pre><code>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 </code></pre> <p>Which results in:</p> <pre><code>'RankingInline.fields' refers to field 'account_type' that is missing from the form. </code></pre> <p>I have also tried using the model__field method, which I used as:</p> <pre><code>fields = ('product', 'product__account_type', 'rank') </code></pre> <p>Which results in:</p> <pre><code>'RankingInline.fields' refers to field 'product__account_type' that is missing from the form. </code></pre> <p>The models are defined as so:</p> <pre><code>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) </code></pre> <p>What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload