Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvitationAdminAddForm doesn't save data for new fields added in customised Invite model #260

Open
JoGorska opened this issue Aug 8, 2024 · 0 comments

Comments

@JoGorska
Copy link
Contributor

JoGorska commented Aug 8, 2024

I added a few extra fields to customise the Invite model and passed the CustomInvite model name to settings.

InvitationAdminChangeForm shows all fields, therefore new extra fields were both shown and saved.

InvitationAdminAddForm wasn't showing new fields, because the original code is very explicit as to which fields it shows. It would be nice if it were showing extra fields. As a solution, I created CustomInvitationAdminAddForm and passed the name of the form to settings. Unfortunately, I originally decided to inherit from InvitationAdminAddForm which caused a different problem:

The second problem I was facing is that InvitationAdminAddForm didn't save data, even when I added these fields to the form and passed a new form name to settings. New fields were now shown in the form, but data wasn't saved.

It seems like the issue may be caused by this approach in the save() method in the original form in invitations.forms.InvitationAdminAddForm

    def save(self, *args, **kwargs):
        cleaned_data = super().clean()
        email = cleaned_data.get("email")
        params = {"email": email}
        if cleaned_data.get("inviter"):
            params["inviter"] = cleaned_data.get("inviter")
        instance = Invitation.create(**params)
        instance.send_invitation(self.request)
        super().save(*args, **kwargs)
        return instance

It seems that instead of creating an Invitation from cleaned_data, it is created from params (email and optional inviter) therefore completely ignoring the new field added to the custom model. I solved this problem by customising save() method and making sure the instance is created with all cleaned_data.

Fixing the problem in save() would allow developers to use invitations in Django admin without customising the form because new fields would be automatically captured.


To summarise: In invitations.forms.InvitationAdminAddForm in save() instance could be created from all cleaned_data instead of **params (inviter and email only). This would be helpful because developers who customise the Invite model in their project, don't have to customise the save() method in this form. They would only need to add fields to the form.

Optionally maybe we could consider adding fields = "__all__" this way no customisation of InvitationAdminAddForm would be required. The developer could customise Invite and have all Django admin functionality working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant