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

Duplicate saves in TurboCreateView #8

Closed
davidkell opened this issue Aug 10, 2021 · 1 comment · Fixed by #9
Closed

Duplicate saves in TurboCreateView #8

davidkell opened this issue Aug 10, 2021 · 1 comment · Fixed by #9

Comments

@davidkell
Copy link
Contributor

davidkell commented Aug 10, 2021

After debugging, we noticed that the form is saved twice in TurboCreateView.

Relevant source code:

# mixins.py
class TurboFormModelMixin(TurboFormMixin):

    object: Optional[Model]

    def form_valid(self, form: forms.Form) -> HttpResponse:
        """If the form is valid, save the associated model."""
        self.object = form.save()
        return super().form_valid(form)

# views.py

class TurboCreateView(TurboFormModelMixin, CreateView):
    ...

But the Django CreateView already uses the ModelFormMixin with:

def form_valid(self, form):
    """If the form is valid, save the associated model."""
    self.object = form.save()
    return super().form_valid(form)

The result is that form.save() is called twice.

I think the fix is:

class TurboCreateView(TurboFormMixin, CreateView):
    ...
```

Ditto `TurboUpdateView`. Thoughts?
@danjac
Copy link
Collaborator

danjac commented Aug 10, 2021

I haven't used the CBVs myself so that might well be the case.

I'd be happy to accept a PR. Key is that the view should return the correct response status (e.g. 303 redirect if successful).

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

Successfully merging a pull request may close this issue.

2 participants