1

I have a model Order and model Invoice. The Order has

invoice = models.OneToOneField('Invoice', related_name='order', on_delete=models.CASCADE, blank=True, null=True)

Invoice object is created right after order object is created an assigned to it. Admin has to edit the invoice (price field) before customer pays.

The problem is that Django-admin allows admin to change this field too (bottom of the image), which I can't risk but I want to let the pencil icon (change attributes of the invoice).

enter image description here

Is it possible to do that? When I add invoice to readonly_fields in OrderAdmin, Admin can't edit those attributes like invoice.price etc.

EDIT:

So I want admin to be able to edit attributes of the invoice. Not add nor choose from dropdown.

Milano
  • 18,048
  • 37
  • 153
  • 353

2 Answers2

1

One option would be to provide a custom template for this view. The docs say that you can specify a path to a custom template using ModelAdmin.change_form_template.

Here is a section of the docs that talk about how to override a template: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#overriding-vs-replacing-an-admin-template

Though this is not the optimal solution, you could probably use Javascript to hide/disable the parts you don't want.

Finally, you may want to consider your usage of the Django admin:

The admin’s recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around.

The admin has many hooks for customization, but beware of trying to use those hooks exclusively. If you need to provide a more process-centric interface that abstracts away the implementation details of database tables and fields, then it’s probably time to write your own views.

denvaar
  • 2,174
  • 2
  • 22
  • 26
0
def has_add_permission(self, request):
    return False

Django admin: How to display a field that is marked as editable=False' in the model?

Community
  • 1
  • 1