3
votes

Django admin for ForeignKey fields renders a small "plus" link that allows to create a new entity.

enter image description here

And it looks like it is also able to render links for change/delete ForeignKey objects

https://github.com/django/django/blob/1.8/django/contrib/admin/templates/admin/related_widget_wrapper.html#L13

But no example from the documentation shows how to make this functionality available. Is there any non documented option?

2
i use django 1.8.4 .when i login with supperuser , i see add and edit links but delete is invisble. - Hasan Ramezani

2 Answers

2
votes

The answer lies in the code of the BaseModelAdmin class and the RelatedFieldWidgetWrapper class.

In BaseModelAdmin, it is first ensured that the user has permission to add, change or delete objects of the related model.

In RelatedFieldWidgetWrapper, to change or delete a related object it is also ensured that the relation is not a many-to-many. Moreover, a related object cannot be deleted if the cascade delete is enabled for this relationship.

The permissions are by default True, I assume you would know if if you had changed them? If indeed you didn't, the reason why you cannot change the related object might be that you overrid the readonly_fields attribute in the admin model, or the get_readonly_fields method.

Once you can see the change button, you will probably not yet see the delete button as the cascading is the default. You can change this by setting the on_delete attribute when creating the foreign key.

2
votes

look at __init__ function of class RelatedFieldWidgetWrapper in django/contrib/admin/widgets.py:

self.can_delete_related = not multiple and not cascade and can_delete_related