I have validation errors in my django formset. Two drop down lists, populated from the database, do not pass the validation, and I don't understand what's my mistake.
model:
class Country(models.Model):
country_code=models.CharField(max_length=2, primary_key=True)
country=models.CharField(max_length=20, unique=True)
def __unicode__(self):
return u"%s" % self.country
class Status(models.Model):
verbatim = models.ForeignKey(Verbatim)
country = models.ForeignKey(Country)
status = models.CharField(max_length=5, db_index=True)
def __unicode__(self):
return u"%s" % self.status
class Meta:
unique_together=(("verbatim", "country"), )
class ImportMinAttend(models.Model):
country=models.CharField(max_length=2, blank=False, null=False)
verbatim=models.CharField(max_length=250, blank=False, null=False)
status=models.CharField(max_length=5, blank=True, null=True, default=None)
form:
class MinAttendForm(forms.ModelForm):
country=forms.ModelChoiceField(queryset=Country.objects.all(), empty_label="Select a country")
status=forms.ModelChoiceField(queryset=Status.objects.values_list('status', flat = True).distinct(), empty_label="Select a status")
class Meta:
model=ImportMinAttend
#fields used for the validation
fields = ('country', 'verbatim', 'status')
view:
class MinAttendUpdate(UpdateView):
model = ImportMinAttend
fields = ['country', 'verbatim', 'status']
form_class=MinAttendForm
def post(self, request, *args, **kwargs):
...
MinAttendFormSet = modelformset_factory(self.model, form=self.form_class, fields=self.fields, extra=len(attendances), max_num=len(attendances)+self.nb_extra_forms)
formset=MinAttendFormSet(request.POST, queryset=attendances)
...
Source code of the first country select:
<select name="form-0-country" id="id_form-0-country">
<option value="">Select a country</option>
<option value="AT" selected="selected">Austria</option>
<option value="BE">Belgium</option>
<option value="BG">Bulgaria</option>
...
Source code of the first status select:
<select name="form-0-status" id="id_form-0-status">
<option value="">Select a status</option>
<option value="AB">AB</option>
<option value="CS">CS</option>
<option value="M" selected="selected">M</option>
</select>
About the country select: the value displayed has more than two characters but the key used has exactly 2 characters. Why this validation error? About the status, I don't even understand the problem...
Many thanks.
I have found "dirty" workarounds. For the country select, I use the key of the select, not the value:
def clean_country(self):
data = self.cleaned_data['country'].pk
return data
For the status select, I delete the validation error if a value is selected:
def clean(self):
#call status clean method
self.cleaned_data["status"]=self.clean_status()
return self.cleaned_data
def clean_status(self):
#valid if a value has been selected
if self["status"].value()!="":
del self._errors["status"]
return self["status"].value()
It works, but why do I have to do this? :(
I think you are doing it the hard way. There is a lot easier way to do it, taking advantage of ModelForm. Here is a full example. Read it and adapt it to your models:
from django.db import models
from django.forms import ModelForm
TITLE_CHOICES = (
('MR', 'Mr.'),
('MRS', 'Mrs.'),
('MS', 'Ms.'),
)
class Author(models.Model):
name = models.CharField(max_length=100)
title = models.CharField(max_length=3, choices=TITLE_CHOICES)
birth_date = models.DateField(blank=True, null=True)
def __str__(self): # __unicode__ on Python 2
return self.name
class Book(models.Model):
name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = ['name', 'title', 'birth_date']
class BookForm(ModelForm):
class Meta:
model = Book
fields = ['name', 'authors']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With