planet260
planet260

Reputation: 1454

Django Admin Panel. Display and Edit Parent Fields in Child's Object (ModelAdmin.inlines reverse)

I am trying to create admin panel. I have three classes in my model.

1) Company
2) Executive
3) CompanyExecutive (Which have one to Many relation between Company and Executive)

I only have to show "CompanyExecutive" on admin panel. But for now when i click on "CompanyExecutive" object i am redirected to the object which has Company Executive relationship saved. What i need to do is when i click on "CompanyExecutive" i have to open "Executive" instance. I am kind of stuck in this issue. Can anyone help me in figuring out how to achieve this task. I have looked at ModelAdmin.inlines but as one can see, the parent/child relationship have to be reversed.

Thanks in advance

Edit 1: I have an approach in mind which is to redirect the user to a different page i-e Executive page when CompanyExecutive instance is clicked. Now the question is which function i need to override in admin class to achieve this task..

Upvotes: 1

Views: 836

Answers (1)

planet260
planet260

Reputation: 1454

I took a different approach. On the main page of CompanyExecutive i added a custom field which is link to the Executive object. This way if user want to change the relation of the CompanyExecutive he can click on the object as usual and if he/she wants to change Executive details he can click on the link. In model i added a custom field

def Person_Link(self):
    return "<a href='/admin/sweetspotModel/persons/%s/' target='_blank'>Person Details<a/>" % self.contactid.contactid
Person_Link.allow_tags = True  

And in Admin panel i displayed this field on the main page.

class ExecutiveAdmin(admin.ModelAdmin):
    list_display = ('Person_Name', 'Person_Link')

I hope this approach helps anyone.

Upvotes: 1

Related Questions