Пример #1
0
 def get_choices(self, filter=None, order_by=None):
     queryset = None
     if self.field_class in ('forms.ModelChoiceField', 'forms.ModelMultipleChoiceField'):
         if filter:
             exec('queryset = ModelNameField.get_model_from_string(self.choice_model).objects.%s' % filter)
         else:
             queryset = ModelNameField.get_model_from_string(self.choice_model).objects.all()
         
         if order_by:
             queryset = queryset.order_by(order_by)
         
         return [FieldChoiceContainer(value=item.id, label=item.title) for item in queryset]
     else:
         return self.choices.order_by('value')
Пример #2
0
    def get_form_field_init_args(self):
        args = {
            'required': self.required,
            'label': self.label if self.label else '',
            'initial': self.initial if self.initial else None,
            'help_text': self.help_text,
        }
        
        if self.field_class in ('forms.CharField', 'forms.EmailField', 'forms.RegexField'):
            args.update({
                'max_length': self.max_length,
                'min_length': self.min_length,
            })

        if self.field_class in ('forms.IntegerField', 'forms.DecimalField'):
            args.update({
                'max_value': int(self.max_value) if self.max_value != None else None,
                'min_value': int(self.min_value) if self.min_value != None else None,
            })

        if self.field_class == 'forms.DecimalField':
            args.update({
                'max_value': self.max_value,
                'min_value': self.min_value,
                'max_digits': self.max_digits,
                'decimal_places': self.decimal_places,
            })

        if self.field_class == 'forms.RegexField':
            if self.regex:
                args.update({
                    'regex': self.regex
                })

        if self.field_class in ('forms.ChoiceField', 'forms.MultipleChoiceField'):
            #print "Choices count:", self.choices.count()
            if self.choices.count():
                # new method of creating choices
                choices = [(choice.value, choice.label) for choice in self.choices.all()]
                args.update({
                    'choices': tuple(choices)
                })
                
                #print "Choices:", choices

        if self.field_class in ('forms.ModelChoiceField', 'forms.ModelMultipleChoiceField'):
            args.update({
                'queryset': ModelNameField.get_model_from_string(self.choice_model).objects.all()
            })
        
        if self.field_class == 'forms.ModelChoiceField':
            args.update({
                'empty_label': self.choice_model_empty_label
            })

        if self.widget:
            args.update({
                'widget': eval(self.widget)()
            })
        
        return args
Пример #3
0
 def to_field_list(self):
     """
     Converts this form definition into a list of dictionaries, each
     dictionary representing a field and its components.
     
     @param fields A list of fields to include. By default, if this is
         None, all fields will be generated.
     @param field_name_replacements
     """
     
     field_arr = []
     
     # run through all of the fields associated with this definition
     for field in self.fields.all():
         choices = []
         if field.choices.count():
             choices = [{'value': u'%s' % choice.value, 'label': u'%s' % choice.label} for choice in field.choices.all()]
         elif field.choice_model:
             choices = [{'value': u'%s' % obj.id, 'label': u'%s' % obj} for obj in ModelNameField.get_model_from_string(field.choice_model).objects.all()]
         
         field_item = {
             'name': u'%s' % field.name,
             'label': u'%s' % field.label,
             'class': u'%s' % field.field_class,
             'position': u'%s' % field.position,
             'widget': u'%s' % field.widget,
             'initial': u'%s' % field.initial,
             'help_text': u'%s' % field.help_text,
         }
         if choices:
             field_item['choices'] = choices