Inserting further fields of a foreignkey object into change list view

24 views
Skip to first unread message

Vittorio

unread,
Oct 21, 2014, 10:29:53 AM10/21/14
to django...@googlegroups.com
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



Collin Anderson

unread,
Oct 21, 2014, 12:00:03 PM10/21/14
to django...@googlegroups.com, ml-...@de-martino.it
Hi Vittorio,

Would this work?

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

   
def patient_address(self, operation):
       
return operation.patient.address

   
def patient_city_zip(self, operation):
       
return '%s %s' % (operation.patient.city, operation.patient.zipcode)

Collin

Collin Anderson

unread,
Oct 21, 2014, 12:00:30 PM10/21/14
to django...@googlegroups.com, ml-...@de-martino.it
oops readonly_field should be readonly_fields

Vittorio

unread,
Oct 21, 2014, 1:25:44 PM10/21/14
to django...@googlegroups.com, Collin Anderson
Great, it works!
Thanks a lot Collin
Ciao from Rome
Vittorio

Victor

unread,
Oct 23, 2014, 7:32:18 AM10/23/14
to django...@googlegroups.com
Dear Experts,
following the indications of Collin I was able to see the extra fields I wanted to show in the change list view. But there's a little problem.
The character size of the labels of the readonly_fields are bigger than those of the printed fields so that the visual effect is that the labels and their relating fields don't look aligned (see the attached png file).
How can I modify the size of readonly_fields characters?
Ciao
Vittorio

Collin Anderson

unread,
Oct 24, 2014, 12:36:43 PM10/24/14
to django...@googlegroups.com
Hello Vittorio,

They don't seem bigger to me, but you should be able to change that by including custom CSS.

Collin

Reply all
Reply to author
Forward
0 new messages