ForeignKey field will not appear in Django admin s

2019-08-02 20:11发布

问题:

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.

I realize there are many variables that could be causing this behavior.

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']

The problem is the advertiser foreign key is not showing up in the admin for AdBase

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 & 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 & end_q).count()

class BannerAd(AdBase):
    """ A standard banner Ad """
    content = models.ImageField(upload_to="adzone/bannerads/")

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...

回答1:

I believe I've just run into exactly the same problem, but was able to debug it thanks to the help of persistent co-workers. :)

In short, if you look in the raw HTML source you'll find the field was always there - it's just that:

  • Django tries to be clever and put the form field inside a div with CSS class="form-row $FIELD_NAME",
  • The field's name was "advertiser", so the CSS class was "form-row advertiser",
  • ...Adblock Plus.

Adblock Plus will hide anything with the CSS class "advertiser", along with a hell of a lot of other CSS classes.

I consider this a bug in Django.



回答2:

maybe it is an encode error. I had the same problem, but when i added # -- coding: UTF-8 -- in the models.py, all fine.



回答3:

Another very dumb cause of the same problem:

If there is only one instance of the related model, then the filter simply won't show. There is a has_output() method in RelatedFieldListFilter class that returns False in this case.



回答4:

Powellc, do you have the models registered with their respective ModelAdmin class?

admin.site.register(Advertiser, AdvertiserAdmin) after the ModelAdmin definitions.



回答5:

You are talking about the list_display option, right?

Is the unicode-method for your related model set?

If the field is a ForeignKey, Django will display the unicode() of the related object

Also check this thread for some hints: Can "list_display" in a Django ModelAdmin display attributes of ForeignKey fields?



回答6:

Try disabling your ad blocker. No, this is not a joke. I just ran into this exact problem.



回答7:

It's a strange problem for sure. On the AdBase model if you change

advertiser = models.ForeignKey(Advertiser)

to

adver = models.ForeignKey(Advertiser)

then I believe it'll show up.



回答8:

We just ran into this problem.

It seems that if you call you field advertiser the in the admin the gets given an 'advertiser' class.

Then is then hidden by standard ad blocking plugins. If you view source your field will be there.