Login

Add validation for 'unique' and 'unique_together' constraints to newforms created dynamically via form_for_model or form_for_instance

Author:
bikeshedder
Posted:
June 3, 2007
Language:
Python
Version:
.96
Score:
3 (after 3 ratings)

This snippet provides a form_for_model and form_for_instance function which create newforms that respect the unique and unique_together constraints defined in the model.

One thing about the coding style in this code snippet: Classes are capitalized even if they're passed as arguments. Thus "Model" is a model class while "model" is a model instance.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def formfield_factory(model_field, **kwargs):
    form_field = model_field.formfield(**kwargs)
    if form_field:
        form_field.model_field = model_field
    return form_field


def _join(l):
    l = list(l)
    return '%s and %s' % (', '.join(l[:-1]), l[-1])


def validate_unique_constraint(form, Model, model, field_names):
    '''Check a single 'unique' or 'unique_together' constraint'''
    filter = {}
    # search for other objects with the same data for the unique fields
    for field_name in field_names:
        if field_name not in form.cleaned_data:
            # No cleaned data for the field means either that the field is
            # nullable and was left empty or that the field itself did not
            # validate.
            return
        filter[field_name] = form.cleaned_data[field_name]
    query_set = Model.objects.filter(**filter)
    # exclude model instance
    if model is not None:
        query_set = query_set.exclude(id=model.id)
    # raise ValidationError if query gives a result
    if query_set.count() > 0:
        if len(field_names) > 1:
            raise ValidationError('The fields %s must be unique together.' \
                % _join('"%s"' % Model._meta.get_field(field_name).verbose_name for field_name in field_names))
        else:
            raise ValidationError('The field "%s" must be unique.' \
                % Model._meta.get_field(field_names[0]).verbose_name)


def validate_unique_constraints(form, Model, model=None):
    '''Check 'unique' and 'unique_together' constraints defined in the Model and Fields.'''
    # check 'unique' constraints defined in the fields
    for field_name in form.fields:
        model_field = Model._meta.get_field(field_name)
        if model_field.unique:
            validate_unique_constraint(form, Model, model, [field_name])
    # check 'unique_together' constraints defined in the model
    unique_together = Model._meta.unique_together
    for field_names in unique_together:
        validate_unique_constraint(form, Model, model, field_names)


def enhance_form(Form, Model, model=None):
    '''Wrap the Form.clean method and add checks for unique constraints.'''
    wrapped_clean = Form.clean
    def clean(form):
        form.cleaned_data = wrapped_clean(form)
        validate_unique_constraints(form, Model, model)
        return form.cleaned_data
    Form.clean = clean


def form_for_model(Model, **kwargs):
    from django.newforms.models import form_for_model
    Form = form_for_model(Model, formfield_callback=formfield_factory, **kwargs)
    enhance_form(Form, Model)
    return Form


def form_for_instance(model, **kwargs):
    from django.newforms.models import form_for_instance
    Form = form_for_instance(model, formfield_callback=formfield_factory, **kwargs)
    enhance_form(Form, type(model), model)
    return Form

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.