Tuesday, October 21, 2014

Inserting further fields of a foreignkey object into change list view

Dear Experts,
Under Django 1.7 I have, among other things, the following:


### models.py
class Patients(models.Model):
surname = models.CharField(max_length=60, db_index=True)
name = models.CharField(max_length=60, db_index=True)
address = models.CharField(max_length=150)
city = models.CharField(max_length=120)
zipcode = models.CharField(max_length=15)
def __unicode__(self):
return u"%s %s" % (self.surname, self.name)
class Meta:
db_table = u'patients'
ordering=['surname', 'name']

class Operation(models.Model):
date_of_operation=models.DateTimeField(db_index=True)
patient=models.ForeignKey(Patients,default=1)
class Meta:
db_table = u'operation'
verbose_name_plural = "Warehouse IN/OUT"
ordering=['-date_of_operation']

class Details(models.Model):
type = models.ForeignKey(Operation)
item_description = models.CharField(maxlength=100)
class Meta:
db_table = u'details'

### admin.py

class DetailsInline(admin.TabularInline):
fields = ('type','item_description')
model=Details

class DetailsOption(admin.ModelAdmin):
list_per_page = 500
list_display = ('type', 'item_description')
fields=(('type', 'item_description'))

class OperationOption(admin.ModelAdmin):
list_display = ('patient','date_of_operation')
fields=('patient','date_of_operation')
inlines=[DetailsInline]
order_by=['-date_of_operation',]


Now, focusing on Operation and its inline DetailsInline, when I go to the admin change list view I see on top the date_of_operation field and the surname and name of the patient (as specified in the def unicode....) followed by the change list of all Items description (as tabularinline) of that specific operation.

Now I would like to have on top of the admin change list view besides the surname and name of the patient also the address, city, zipcode of that patient. As you can see these pieces of info are fields of the table used as ForeignKey (see model Patients).

What is the quickest and neat way of getting this result?

Ciao
Vittorio



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/096D8757-913D-4A2F-AD28-BA0F0364F3A7%40de-martino.it.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment