Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Django Admin - list_display doesn't follow FK links? [RSS Feed]

#1 Jan. 29, 2011 12:29:00

Victor H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django Admin - list_display doesn't follow FK links?


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 =


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:http://stackoverflow.com/questions/163823/can-list-display-in-a-django-modeladmin-display-attributes-of-foreignkey-fieldsAnd a ticket for it her:http://code.djangoproject.com/ticket/5863but discussion seems to have petered off. Is there a technical or design
reason for this?

Cheers,
Victor

--
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+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 29, 2011 12:43:42

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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 =
>
>
> 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:
>
>
>http://stackoverflow.com/questions/163823/can-list-display-in-a-django-modeladmin-display-attributes-of-foreignkey-fields>
> And a ticket for it her:
>
>http://code.djangoproject.com/ticket/5863>
> 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+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 29, 2011 20:11:20

Excite U.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django Admin - list_display doesn't follow FK links?


Hey Victor,

I do the following to get data out of a related table/model:

Of course you could access your other model via __unicode__...
Or, simply write a function for your model and use a callback in your
list_display:

Here an easy "quick and dirty" example:

class othermodel(models.Model)
importantfield=models.CharField(....)

class mymodel(models.Model)
value = models.CharField(....)
FK_othermodel= models.Foreignkey(othermodel)

def get_importantfield(self):
return self.FK_othermodel.importantfield


In your admin.py call

MymodelAdmin(admin.ModelAdmin)
list_display=

Hope it helps!

Mathieu

--
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+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 29, 2011 22:46:23

Mathieu M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django Admin - list_display doesn't follow FK links?


Hey Victor,

---------------------I changed my subscription to my gmail-account because I'm not sure if myemail went trough. Here is it again.---------------------

I do the following to get data out of a related table/model:

Of course you could access your other model via __unicode__...
Or, simply write a function for your model and use a callback in your
list_display:

Here an easy "quick and dirty" example:

class othermodel(models.Model)
importantfield=models.CharField(....)

class mymodel(models.Model)
value = models.CharField(....)
FK_othermodel= models.Foreignkey(othermodel)

def get_importantfield(self):
return self.FK_othermodel.importantfield

In your admin.py call

MymodelAdmin(admin.ModelAdmin)
list_display=

Hope it helps!

Mathieu

--
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+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Django Admin - list_display doesn't follow FK links? [RSS Feed]

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support