I am not understanding why this is happening. I have apply the exact same code to other inline formsets but this specific inline formset is not saving any of my changes. I found that within my form "AtLeastOneFull" that form.cleaned_data is always empty despite that I have data.
The webpage displays the contents of model 'Access' correctly, but once I click submit, it registers as invalid because I have no data regardless of the changes I make to the inline form's data on the web page.
forms:
class AtLeastOneFull(forms.models.BaseInlineFormSet):
def clean(self):
count = 0
for form in self.forms:
try:
if form.cleaned_data and not form.cleaned_data.get('access_rights', ACCESS_CHOICES[0][0]):
count += 1
assert False
except AttributeError:
pass
if count < 1:
raise forms.ValidationError('You must have at least one full access user')
class UserAccessForm(forms.ModelForm):
class Meta:
model = Access
def clean(self):
cleaned_data = self.cleaned_data
# Check 1: Must have valid user.
# To Be Developed
return cleaned_data
models:
class Portfolio (models.Model):
nickname = models.CharField(max_length=20, unique=True)
name = models.CharField(max_length=50, unique=True)
address1 = models.CharField(max_length=75, null=True, blank=True) #Street address, P.O. box, company name, c/o
address2 = models.CharField(max_length=75, null=True, blank=True) #Apartment, suite, unit, building, floor, etc.
city = models.CharField(max_length=30, null=True, blank=True)
state = models.CharField(max_length=2, null=True, blank=True)
zip = models.CharField(max_length=10, null=True, blank=True)
def __unicode__(self):
return u'%s' % (self.nickname)
class Meta:
ordering = ['name']
# Property Expansion
class Access (models.Model):
portfolio_id = models.ForeignKey(Portfolio)
user_id = models.ForeignKey(User)
title = models.CharField(max_length=30, null=True, blank=True)
access_rights = models.PositiveIntegerField(choices=ACCESS_CHOICES)
def __unicode__(self):
return u'%s: %s' % (self.portfolio_id, self.user_id)
class Meta:
ordering = ['portfolio_id', 'user_id']
unique_together = ("portfolio_id", "user_id")
view:
cPortfolio = Portfolio.objects.get(nickname=pNickname)
AccessFormSet = inlineformset_factory(Portfolio,
Access,
form=UserAccessForm,
formset=AtLeastOneFull,
extra=1,
can_delete=False)
if request.method == 'POST':
if 'access_apply' in request.POST:
cAccessFormSet = AccessFormSet(request.POST, request.FILES, instance=cPortfolio)
if cAccessFormSet.is_valid():
testResults = cAccessFormSet.save(commit=False)
for form in testResults:
form.save()
cAccessFormSet = AccessFormSet(instance=cPortfolio)
Surely it's not as simple as removing the assert False
statement in AtLeastOneFull.clean
?
Also, why is UserAccessForm.clean
querying User
s by "Username"
, when the model field is actually user_id
?