Login

Check condition in Form Queryset

Author:
felipecruz
Posted:
December 2, 2011
Language:
Python
Version:
1.2
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.