Re: InlineAdmin unable to delete object with read only primary key

2022-11-23 Thread Gagan Deep
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 

On Tuesday, November 22, 2022 at 12:03:28 PM UTC+5:30 
shang.xia...@gmail.com wrote:

> 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
>
> On Tue, 22 Nov 2022 at 04:42, Gagan Deep  wrote:
>
>> 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. 
>>
>> [image: 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 
>>
>> -- 
>> 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
>>  
>> 
>> .
>>
>

-- 
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-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/49d2bd1e-7adc-49a9-bff6-a8f873ee686cn%40googlegroups.com.


Calling save_m2m() in UserCreationForm.save()

2022-11-23 Thread Mark Gensler
Hello all!

I noticed that when calling contrib.auth.forms.UserCreationForm.save(), 
self.save_m2m() is not called when commit=True.

This caused an issue in one of my projects which uses a custom User model 
with ManyToMany fields. Is this something that should be changed? I saw in 
the docs [1] that it is advised to extend UserCreationForm and 
UserChangeForm if using a custom User model.

A few reasons I see to add this step to the UserCreationForm.save() method:

   - UserCreationForm is a subclass of ModelForm, which does call 
   save_m2m() when commit=True.
   - UserChangeForm *does* call save_m2m() as part of save(), because the 
   save() method is not overloaded. This seems inconsistent!

The solution I'd propose is:

class UserCreationForm(forms.ModelForm):
...
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
*if hasattr(self, "save_m2m"):*
*self.save_m2m()*
return user

I'd be happy to raise a ticket and work on a patch if this change would be 
useful.

Thanks
Mark

[1] 
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#custom-users-and-the-built-in-auth-forms

-- 
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-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/c8dac66b-4a0f-4afa-b548-260fffb06e9fn%40googlegroups.com.