Last active
June 10, 2024 15:30
-
-
Save neara/6209563 to your computer and use it in GitHub Desktop.
Django Class Based Views and Inline Formset Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.forms import ModelForm | |
from django.forms.models import inlineformset_factory | |
from models import Sponsor, Sponsorship | |
class SponsorForm(ModelForm): | |
class Meta: | |
model = Sponsor | |
class SponsorshipForm(ModelForm): | |
class Meta: | |
model = Sponsorship | |
SponsorShipsFormSet = inlineformset_factory(Sponsor, Sponsorship, | |
form=SponsorshipForm, extra=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import transaction | |
from django.views.generic import CreateView | |
from forms import SponsorForm, SponsorShipsFormSet | |
class CreateSponsor(CreateView): | |
form_class = SponsorForm | |
template_name = 'sponsor_form.html' | |
def get_context_data(self, **kwargs): | |
data = super(CreateSponsor, self).get_context_data(**kwargs) | |
if self.request.POST: | |
data['sponsorships'] = SponsorShipsFormSet(self.request.POST) | |
else: | |
data['sponsorships'] = SponsorShipsFormSet() | |
return data | |
def form_valid(self, form): | |
context = self.get_context_data() | |
sponsorships = context['sponsorships'] | |
with transaction.commit_on_success(): | |
form.instance.created_by = self.request.user | |
form.instance.updated_by = self.request.user | |
self.object = form.save() | |
if sponsorships.is_valid(): | |
sponsorships.instance = self.object | |
sponsorships.save() | |
return super(CreateSponsor, self).form_valid(form) | |
def get_success_url(self): | |
return reverse('sponsors') |
can you show us sponsor_form.html ?
Interesting and helpful, but is SponsorMixin specific to the case you used for the example or is it specific to the usage of inline_formsets
with CreateView
?
This thing is old now module 'django.db.transaction' has no attribute 'commit_on_success'
SponsorForm get saved even though SponsorShipsFormSet are invalid
Maybe this update helps:
def form_valid(self, form):
context = self.get_context_data()
sponsorships = context['sponsorships']
with transaction.atomic():
form.instance.created_by = self.request.user
form.instance.updated_by = self.request.user
self.object = form.save()
if sponsorships.is_valid():
sponsorships.instance = self.object
sponsorships.save()
return super(CreateSponsor, self).form_valid(form)
What the hell is SponsorMixin?
He's not actually using that in his code but it's basically an input parameter.
Do you guys know how to implement update_or_create method for inline model to exclude possibility of adding 2 or more similar instances?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My site is in error in
UpdateView
form show duplicate

forms.py
views.py
urina_rotina_update.html