Login

Check condition in Form Queryset

Author:
felipecruz
Posted:
December 2, 2011
Language:
Python
Version:
1.2
Tags:
form test condition check
Score:
1 (after 1 ratings)

Check a condition (in this example only equality) on every queryset item in a django form

 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
''' 
    example:

        form = response.context['myform']
        subsidiary = ....

        self.assertTrue(
                FormTestHelper.check_in_queryset(
                    form,
                    'attribute',
                    property='subsidiary',
                    type='=',
                    against=subsidiary
                )
            )
'''


def check_condition(element1, **condition):
    #fill this code with your conditions!!!

    if condition.has_key('condition') and condition['type'] == '=':
        if condition.has_key('property'):
            return getattr(element1, 
                        condition['property']) == condition['against']
        else:
            return element1 == condition['against']
    return False

class FormTestHelper(object):
    @staticmethod
    def check_in_queryset(form, form_element, **condition):
        return all([check_condition(element, **condition) for element in
                                form.fields[form_element].queryset])

More like this

  1. Validate by file content type and size by macmichael01 6 years, 3 months ago
  2. Conditional url pattern include by zbyte64 4 years, 9 months ago
  3. Override QuerySet.delete() (one way of preventing cascading deletes) by timbroder 5 years, 4 months ago
  4. Easy Conditional Template Tags by fragsworth 5 years, 11 months ago
  5. Lazy options on ModelForm fields - like setting a ModelChoiceField queryset from the view by jpic 6 years, 4 months ago

Comments

Please login first before commenting.