Inline-like solution for Django Admin where Admin contains ForeignKey to other model

拜拜、爱过 提交于 2019-11-27 18:36:37
Udi

Completing @John's answer from above - define what you would like to see on the your changelist:

return '<a href="%s">%s</a>' % (
                     reverse('admin:applabel_customer_change', (self.customer.id,)),
                     self.customer.name # add more stuff here
             )

And to add this to the change form, see: Add custom html between two model fields in Django admin's change_form

John

There is no easy way to do this with django. The inlines are designed to follow relationships backwards.

Potentially the best substitute would be to provide a link to the user object. In the list view this is pretty trivial:

Add a method to your appointment model like:

def customer_admin_link(self):
    return '<a href="%s">Customer</a>' % reverse('admin:app_label_customer_change %s') % self.id
customer_admin_link.allow_tags = True
customer_admin_link.short_description = 'Customer'

Then in your ModelAdmin add:

list_display = (..., 'customer_admin_link', ...)

Another solution to get exactly what you're looking for at the cost of being a bit more complex would be to define a custom admin template. If you do that you can basically do anything. Here is a guide I've used before to explain: http://www.unessa.net/en/hoyci/2006/12/custom-admin-templates/

Basically copy the change form from the django source and add code to display the customer information.

In the ModelAdmin class for your Appointments, you should declare the following method:

class MySuperModelAdmin(admin.ModelAdmin):
  def get_form(self, request, obj=None, **kwargs):

    if obj:
      # create your own model admin instance here, because you will have the Customer's
      # id so you know which instance to fetch
      # something like the following
      inline_instance = MyModelAdminInline(self.model, self.admin_site)
      self.inline_instances = [inline_instance]

    return super(MySuperModelAdmin, self).get_form(request, obj, **kwargs)

For more information, browser the source for that function to give you an idea of what you will have access to.

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L423

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!