Saturday, January 29, 2011

Re: 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=['get_importantfield']

Hope it helps!

Mathieu

No comments:

Post a Comment