InlineAdmin unable to delete object with read only primary key

66 views
Skip to first unread message

Gagan Deep

unread,
Nov 21, 2022, 12:42:49 PM11/21/22
to Django developers (Contributions to Django itself)

Hello everyone! 

In my project, I have created a model (Token) which uses a custom primary key (i.e. it uses a field defined by the model for the primary key instead of using "id"). I created an InlineAdmin class for this model and added the primary key field to InlineAdmin.readonly_fields. This InlineAdmin is added to ModelAdmin.inlines of another model.

After making these changes, it is not possible to delete a Token object from the InlineAdmin (web interface). After selecting the delete checkbox for the Token object and clicking on the "Save and continue" button (of ModelAdmin), the page reloads with the Token object still there. 

I have created a simple Demo project to replicate this issue,  https://github.com/pandafy/inline_admin_pk_bug.  

I have done some initial debugging, and found that when the primary key is added to the InlineAdmin.readonly_fields, an HTML input element for that field is not created. 

This does not occur when a model has "id" field for the primary key and the "id" field is added to InlineAdmin.readonly_fields. 


Screenshot from 2022-11-21 23-05-22.png

I believe this is inconsistent behaviour. I will be more than happy to open an issue for this on Trac and work on a potential fix if this is a bug.

Regards, 
Gagan Deep 

David Sanders

unread,
Nov 22, 2022, 1:33:28 AM11/22/22
to django-d...@googlegroups.com
Hi Gagan,

Interesting quirk you've found there, it's possible it could be a bug though further digging may be required.

A couple of interesting notes I found while fiddling with your example code:
  • If you press "Save and continue editing" there are unspecified form errors
  • Setting `editable=False` on the token key fixes these unspecified errors and also allows you to delete the token
  • Note that while inline works with `editable=False` it also means that you can't create a disabled token because of the nature of how checkboxes are handled. This is a separate issue.

David

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/85d9b570-d3dc-46ca-8976-8ffcff061c01n%40googlegroups.com.

Gagan Deep

unread,
Nov 23, 2022, 10:45:11 AM11/23/22
to Django developers (Contributions to Django itself)
Hi David! 

Thanks for double checking my work. Yes, I also see those errors in the admin. I was guessing that those were related to the primary key issue. 

Gagan Deep 

Reply all
Reply to author
Forward
0 new messages