Saturday, January 29, 2011

Re: Django Admin - list_display doesn't follow FK links?

On Saturday, January 29, 2011 12:28:49 PM UTC, Victor Hooi wrote:
Hi,

I'm trying to use list_display in the django-admin, and I can't seem to find a way to get list_display to follow FK links.

My models.py:
class Site(models.Model):
    name = models.CharField(max_length=50, unique=True)
    description = models.TextField()
    
    def __unicode__(self):
        return u'%s' % (self.name
class AccommodationFeature(models.Model):
    name = models.CharField(max_length=50, unique=True)
    description = models.TextField() 
class AccommodationRoom(models.Model):
    name = models.CharField(max_length=50)
    site = models.ForeignKey(Site)
    features = models.ManyToManyField(AccommodationFeature, null=True, blank=True)
    
    def __unicode__(self):
        return u'%s' % (self.name
class BedAvailability(models.Model):
    number_of_single_beds = models.IntegerField()
    number_of_double_beds = models.IntegerField()
    conference = models.ForeignKey(Conference)
    accommodation_room = models.ForeignKey(AccommodationRoom)
    
    class Meta:
        verbose_name_plural = 'bed availabilities'
        
    def __unicode__(self):
        return u'Availability for %s at %s' % (self.accommodation_room, self.conference)
    # Surely this isredundant? hmm, list_dispaly doesn't seem to follow foreignkey relationships?
    def site_name(self):
        return u'%s' % (self.accommodation_room.site.name)
Each "Site" has multiple "AccommodationRooms", which in turn have "BedAvailabilities".

In the BedAvailabilitiesAdmin, for list_display, I'm trying to follow self.accommodation_room.site. This doesn't work using either dot notation or double-underscore notation.

Other ModelAdmin options seem to allow following FK links, using the double underscore notation.

E.g. ModelAdmin.search_fields allows you to search across FK's using:
search_fields = ['foreign_key__related_fieldname']

list_filter also seems to allow the same behaviour.
 
At the moment, I'm just using a function on BedAvailabilities to follow the relationships and print the site name. However, this seems a bit silly when logic would dictate that list_display behaves the same as search_fields and list_filter.
 
Is there any reason this feature doesn't work for list_display?

There's some discussion of it on SO:


And a ticket for it her:


but discussion seems to have petered off. Is there a technical or design reason for this?

Cheers,
Victor


The SO discussion you link to has several answers that give the correct workaround - create a method on the ModelAdmin that returns the value of the followed foreignkey.
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment