Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing Related Fields in Django ModelAdmin

I'm listing a model in Django's admin via a TabularInline. Inside this inline, I'd like to use Django's model traversal syntax to list data in other models referenced from this model via foreign keys. e.g.

class MyRelatedModel(models.Model):
    name = models.CharField(max_length=50)
    created = models.DateField(auto_now_add=True)
    other = models.ForeignKey('MyOtherRelatedModel')

class MyOtherRelatedModel(models.Model):
    name = models.CharField(max_length=50)
    created = models.DateField(auto_now_add=True)
        
class MyRelatedModelInline(admin.TabularInline):
    model = MyRelatedModel
    fields = ['name', 'created', 'other__name']
    #readonly_fields = ['name', 'created', 'other__name']

However, the usage of 'other__name' throws the ImproperlyConfigured error:

'MyRelatedModelInline.fields' refers to field 'other__name' that is missing from the form

Is the model traversal syntax not supported in ModelAdmin instances? If it is supported, what am I doing wrong?

EDIT: If I uncomment readonly_fields, the error becomes:

Caught AttributeError while rendering: 'MyMainModelAdmin' object has no attribute '__name__'
like image 570
Cerin Avatar asked Apr 15 '11 15:04

Cerin


1 Answers

As per Roseman's suggestion above, you can display related (one-to-one or many-to-one) data in a readonly manner with Inline admin models. Here's a little example, just to make sure that we are all on the same page. You can see below that there are three ways to achieve your goal (if I understand that goal correctly).

models.py:

class Company(models.Model):
    name = models.CharField(max_length=50)

class Employee(models.Model):
    name = models.CharField(max_length=50)
    company = models.ForeignKey('Company')
    car = models.ForeignKey('Car')

    def model_callable(self):
        return self.car.rego

class Car(models.Model):
    rego = models.CharField(max_length=50)

admin.py:

def unbound_callable(emp):
    return emp.car.rego

class EmployeeInline(admin.TabularInline):
    model = Employee
    fields = ('name', 'model_callable', 'model_admin_callable', unbound_callable)
    readonly_fields = ('model_callable', 'model_admin_callable', unbound_callable)

    def model_admin_callable(self, emp):
        return emp.car.rego

class CompanyAdmin(admin.ModelAdmin):
    model = Company
    inlines = (EmployeeInline,)

admin.site.register(Company, CompanyAdmin)

As you can see, 'readonly_fields' is treated in the same manner as 'list_display' as per the Django documentation for contrib.admin (from 1.2 onwards).

In the above example, when you are editing a Company, you will see its employees inlined. Each row will have an employee name in an editable textbox and next to the name you will see a readonly bit of text for the employee's car's rego (emp.car.rego).

Referring to your original question, you wanted to reference the related data as 'other__name'. This won't work. Expressions like other__name or car__rego only have special meaning as keyword arguments in filters when running Django queries. For example, when fetching an employee who has a car with a particular rego number:

Employee.objects.filter(car__rego='111')

Hope that helps.

j

like image 78
jsw Avatar answered Oct 16 '22 12:10

jsw