Note that there are some explanatory texts on larger screens.

plurals
  1. POForeignKey field will not appear in Django admin site
    text
    copied!<p>A foreign key on a model is not appearing in the Django admin site. This is irrespective of whether the field is explicitly specified in a ModelAdmin instance (fields = ('title', 'field-that-does-not-show-up')) or not. </p> <p>I realize there are many variables that could be causing this behavior.</p> <pre><code>class AdvertiserAdmin(admin.ModelAdmin): search_fields = ['company_name', 'website'] list_display = ['company_name', 'website', 'user'] class AdBaseAdmin(admin.ModelAdmin): list_display = ['title', 'url', 'advertiser', 'since', 'updated', 'enabled'] list_filter = ['updated', 'enabled', 'since', 'updated', 'zone'] search_fields = ['title', 'url'] </code></pre> <p>The problem is the advertiser foreign key is not showing up in the admin for AdBase</p> <pre><code>class Advertiser(models.Model): """ A Model for our Advertiser """ company_name = models.CharField(max_length=255) website = models.URLField(verify_exists=True) user = models.ForeignKey(User) def __unicode__(self): return "%s" % self.company_name def get_website_url(self): return "%s" % self.website class AdBase(models.Model): """ This is our base model, from which all ads will inherit. The manager methods for this model will determine which ads to display return etc. """ title = models.CharField(max_length=255) url = models.URLField(verify_exists=True) enabled = models.BooleanField(default=False) since = models.DateTimeField(default=datetime.now) expires_on=models.DateTimeField(_('Expires on'), blank=True, null=True) updated = models.DateTimeField(editable=False) # Relations advertiser = models.ForeignKey(Advertiser) category = models.ForeignKey(AdCategory) zone = models.ForeignKey(AdZone) # Our Custom Manager objects = AdManager() def __unicode__(self): return "%s" % self.title @models.permalink def get_absolute_url(self): return ('adzone_ad_view', [self.id]) def save(self, *args, **kwargs): self.updated = datetime.now() super(AdBase, self).save(*args, **kwargs) def impressions(self, start=None, end=None): if start is not None: start_q=models.Q(impression_date__gte=start) else: start_q=models.Q() if end is not None: end_q=models.Q(impression_date__lte=end) else: end_q=models.Q() return self.adimpression_set.filter(start_q &amp; end_q).count() def clicks(self, start=None, end=None): if start is not None: start_q=models.Q(click_date__gte=start) else: start_q=models.Q() if end is not None: end_q=models.Q(click_date__lte=end) else: end_q=models.Q() return self.adclick_set.filter(start_q &amp; end_q).count() class BannerAd(AdBase): """ A standard banner Ad """ content = models.ImageField(upload_to="adzone/bannerads/") </code></pre> <p>The mystery deepens. I just tried to create a ModelForm object for both AdBase and BannerAd, and both generated fields for the advertiser. Some crazy admin things going on here...</p>
 

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