I have created a simple html form using models.Model in models.py, ModelForm in forms.py and oldschool Django templates (nothing fancy)
If a user inputs text in a number field and submits the form, the server returns a KeyError
Example: The following field 'height':
class DataRegistryPart2(models.Model):
height = models.SmallIntegerField(blank=True, null=True)
Will be displayed as a html input in the template:
<input id="id_height" name="height" type="number">
Im using custom validation in forms.py:
...
def clean(self):
cleaned_data = super(CreatePatient, self).clean()
height = cleaned_data["height"] # <-- This is where the KeyError happens!
If a user enters text or a decimal number in the input field and submits the html form, Django returns a KeyError.
How can I prevent this from happening (without using try/catch).
Thanx!
If you enter non-valid values into a field, that field is by definition not clean, so it will not be present in cleaned_data; hence the KeyError.
Unless you need this value to do some validation in combination with another field, you should do whatever you are doing in the specific clean_height method; this will only be called if the value passes the built-in field validation.
Related
I have a subclass of ModelForm, FamilyDemographicsForm, for which two ChoiceFields are required: point_of_contact and birth_parent. For example, the following tests pass:
class FamilyDemographicsFormTest(TestCase):
def test_empty_form_is_not_valid(self):
'''The choice fields 'point_of_contact' and 'birth_parent' are
the only two required fields of the form'''
form = FamilyDemographicsForm(data={})
# The form is not valid because the required fields have not been provided
self.assertFalse(form.is_valid())
self.assertEqual(form.errors,
{'point_of_contact': ['This field is required.'],
'birth_parent': ['This field is required.']})
def test_form_with_required_fields_is_valid(self):
'''The form's save() method constructs the expected family'''
data = {'point_of_contact': Family.EMPLOYEE,
'birth_parent': Family.PARTNER}
form = FamilyDemographicsForm(data=data)
self.assertTrue(form.is_valid())
# The family returned by saving the form has the expected attributes
family = form.save()
self.assertEqual(family.point_of_contact, Family.EMPLOYEE)
self.assertEqual(family.birth_parent, Family.PARTNER)
# The family exists in the database
self.assertTrue(Family.objects.filter(id=family.id).exists())
In the second test case, a new instance of Family is created upon form.save(). I'd like to try instead to update an existing family. To get me started, I tried the following:
def test_update_existing_family(self):
initial = {'point_of_contact': Family.EMPLOYEE,
'birth_parent': Family.PARTNER}
data = {'employee_phone': '4151234567',
'employee_phone_type': Family.IPHONE,
'partner_phone': '4157654321',
'partner_phone_type': Family.ANDROID}
form = FamilyDemographicsForm(data=data, initial=initial)
import ipdb; ipdb.set_trace()
However, when I dropped into the debugger, I noticed that form.is_valid() is False and form.errors indicates that the required fields are not provided:
ipdb> form.errors
{'point_of_contact': ['This field is required.'], 'birth_parent': ['This field is required.']}
My question is: is there any way to instantiate a valid ModelForm with data that does not include the required fields? E.g. by providing an appropriate initial or instance argument? (This is not immediately clear to me from the source code for BaseModelForm on https://github.com/django/django/blob/master/django/forms/models.py).
You can modify the ModelForm and Form that Django provides to fit them your needs. You can override every method as you wish. The most base and essential prefill would be providing an initial dict with data {field_name: value, ...} that the form accepts without any modification.
So for example you have this
class Model1(models.Model):
name = models.CharField(max_length=100)
and this form
class Model1ModelForm(forms.ModelForm):
class Meta:
model = Model1
fields = ('name', )
and you can provide the initial data in the view as
initial = {'name': 'Initial name'}
form = Model1ModelForm(initial=initial)
so name in this form will be prefilled.
django docs: Providing initial values
stack overflow: Pass initial value to a modelform in django
I have a model form defined in my application, and in one of my form fields, I want the users to enter their inputs, but with the first letter of their inputs in capital. If that's not the case, the application should throw an error and prompt the user to re-enter that particular form entry.
Is there an in-built Django form validator for implementing such a restriction?
PS: I am aware of the capitalize() method provided by ModelForm which stores the form field in database with first letter capitalized. However, this won't work for me, since I want to validate the input BEFORE form submission.
from django.db import models
from django.core.exceptions import ValidationError
def validate_capitalized(value):
if value != value.capitalize():
raise ValidationError('Invalid (not capitalized) value: %(value)s',
code='invalid',
params={'value': value})
class MyModel(models.Model):
name = models.CharField(max_length=50, validators=[validate_capitalized])
You can customize ValidationError for your needs. Docs: validators, ValidationError.
You could use validators on the form or model fields, see the Documentation: Using validators and Writing validators.
Reference a simple callable that does the check and raise an exception.
I have a Django model with a customer_code field that is set to unique but only some users will have an assigned value for this field. Other users simply use this code to find the user who provided their code as a reference number. When they submit the form however it raises an error as the field is set to unique.
I would like to remove this error upon validation. The user does not get saved with the value it is set to None before save. I have tried doing this so far with a custom clean() method on the form:
def clean(self):
super(EmployeeForm, self).clean()
if 'customer_code' in self.errors:
del self._errors['customer_code']
return self
But this has not been working. All help is appreciated, thanks.
In the end of method you should return cleaned_data
cleaned_data = super(EmployeeForm, self).clean()
...
return cleaned_data
In my application I have a CreateView that must initialize some fields of the model with a default value, different from the default defined inside the model.
I do not want the user to edit the value, thus I put the field in the exclude list
class AForm(ModelForm):
class Meta:
model = AModel
exclude = ['a_field']
class AView(CreateView):
form_class = AForm
The question is: where do I set the value of a_field?
I tried to define clean methods inside AForm, like thus
class AForm(ModelForm):
[...]
def clean(self):
d = super(AForm, self).clean()
d['a_field'] = 10
return d
however a_field is set to whatever its default value defined in the model is, rather than 10. I also tried defining clean_a_field, but that is simply not executed.
If I remove a_field from the exclude list, then the clean and clean_a_field will work, but the form won't validate unless I render some <input name="a_field"> inside the template, which is not optimal.
I managed to solve the issue in a way that makes me satisfied, although I'm still not 100% happy with the code.
a_field is a required (by the model) field, thus it is necessary to render an <input name="a_field"> inside the template. The trick was to make a_field non-required:
class AForm(ModelForm):
a_field = Field(required=False,
widget=forms.HiddenInput)
class Meta:
model = AModel
def clean_a_field(self):
return 10
This way I can avoid rendering the field in my template, and the form will still validate. And even if the form is rendered with {{ form.as_p }}, widget=forms.HiddenInput saves my day.
Exclude the field from the form, then in the view you can set the value before you save the form:
form = AForm(request.POST)
if form.is_valid():
new_record = form.save(commit=False)
new_record.a_field = 10
new_record.save()
You also might want to avoid the exclude list and specify which fields you'd like to include with the fields attr of the form definition. See the first paragraph here in the docs.
You set a default value in the model. From the official document,
a_field = models.CharField(max_length=7, default=''), for example
I have a way to Face this situation. Follow the following process:
Remove 'a_field' from the excludes in AForm.
Do not expose 'a_field' in HTML template. i.e. Don't give the user option to change the value via Form in Template. This would ensure that normal user's wont modify the value.
To prevent completely, over-ride get_form_kwargs in the View.
This would provide or over-ride your desired value to 'a_field' and save that
e.g.
class AView(CreateView):
form_class = AForm
def get_form_kwargs(self):
kwargs = super(AView, self).get_form_kwargs()
if self.request.method in {'POST', 'PUT'}:
# Change post data to over-ride or provide value of 'a_field'
data = self.request.POST.copy()
data.update({
'a_field': 'value'
})
kwargs['data'] = data
return kwargs
In Django, how do I associate a Form with a Model so that data entered into the form are inserted into the database table associated with the Model? How do I save that user input to that database table?
For example:
class PhoneNumber(models.Model):
FirstName = models.CharField(max_length=30)
LastName = models.CharField(max_length=30)
PhoneNumber = models.CharField(max_length=20)
class PhoneNumber(forms.Form):
FirstName = forms.CharField(max_length=30)
LastName = forms.CharField(max_length=30)
PhoneNumber = forms.CharField(max_length=20)
I know there is a class for creating a form from the the model, but even there I'm unclear on how the data actually gets to the database. And I'd like to understand the inner workings before I move on to the time-savers. If there is a simple example of how this works in the docs, I've missed it.
Thanks.
UPDATED:
To be clear -- I do know about the ModelForm tool, I'm trying to figure out how to do this without that -- in part so I can better understand what it's doing in the first place.
ANSWERED:
With the help of the anwers, I arrived at this solution:
Form definition:
class ThisForm(forms.Form)
[various Field assignments]
model = ThisModel()
Code in views to save entered data to database:
if request_method == 'POST':
form = ThisForm(request.POST)
if form.is_valid():
for key, value in form.cleaned_data.items():
setattr(form.model, key, value)
form.model.save(form.model)
After this the data entered in the browser form was in the database table.
Note that the call of the model's save() method required passage of the model itself as an argument. I have no idea why.
CAVEAT: I'm a newbie. This succeeded in getting data from a browser to a database table, but God only knows what I've neglected or missed or outright broken along the way. ModelForm definitely seems like a much cleaner solution.
Back when I first used Forms and Models (without using ModelForm), what I remember doing was checking if the form was valid, which would set your cleaned data, manually moving the data from the form to the model (or whatever other processing you want to do), and then saving the model. As you can tell, this was extremely tedious when your form exactly (or even closely) matches your model. By using the ModelForm (since you said you weren't quite sure how it worked), when you save the ModelForm, it instantiates an object with the form data according to the model spec and then saves that model for you. So all-in-all, the flow of data goes from the HTML form, to the Django Form, to the Django Model, to the DB.
Some actual code for your questions:
To get the browser form data into the form object:
if request.method == 'POST':
form = SomeForm(request.POST)
if form.is_valid():
model.attr = form.cleaned_data['attr']
model.attr2 = form.cleaned_data['attr2']
model.save()
else:
form = SomeForm()
return render_to_response('page.html', {'form': form, })
In the template page you can do things like this with the form:
<form method="POST">
{{ form.as_p }}
<input type="submit"/>
</form>
That's just one example that I pulled from here.
I'm not sure which class do you mean. I know that there were a helper, something like form_for_model (don't really remember the exact name; that was way before 1.0 version was released). Right now I'd it that way:
import myproject.myapp.models as models
class PhoneNumberForm(forms.ModelForm):
class Meta:
model = models.PhoneNumber
To see the metaclass magic behind, you'd have to look into the code as there is a lot to explain :]. The constructor of the form can take instance argument. Passing it will make the form operate on an existing record rather than creating a new one. More info here.
I think ModelForm.save documentation should explain it. With its base class (Form) you would need to use the Form.cleaned_data() to get the field values and set them to appropriate Model fields "by hand". ModelForm does all that for you.
The Django documentation is pretty clear on this subject. However, here is a rough guide for you to get started: You can either override the form's save method or implement that functionality in the view.
if form.is_valid() # validation - first the fields, then the form itself is validated
form.save()
inside the form:
def save(self, *args, **kwargs):
foo = Foo()
foo.somefield = self.cleaned_data['somefield']
foo.otherfield = self.cleaned_data['otherfield']
...
return foo.save()