def operatorFormField(mymodel, field, widget): """ Returns form field to choose an search operator for this model field """ rangeOperators = (('IN~', 'IN~'), ('IN', 'IN'), ('NOT IN', 'NOT IN')) categoricalOperators = (('=', '='), ('!=', '!=')) textOperators = (('=', '='), ('!=', '!=')) if widget is 'pulldown': return forms.ChoiceField(choices=categoricalOperators, initial=categoricalOperators[0][0], required=True) elif widget is 'textbox': return forms.ChoiceField(choices=textOperators, initial=textOperators[0][0], required=True) elif ordinalField(mymodel, field): return forms.ChoiceField(choices=rangeOperators, initial=rangeOperators[0][0], required=True) elif isinstance(field, (models.AutoField, models.CharField, models.TextField)) or isOrdinalOveridden(mymodel, field): return forms.ChoiceField(choices=textOperators, initial=textOperators[0][0], required=True) elif isinstance(field, (models.BooleanField, models.NullBooleanField)): return forms.ChoiceField(choices=categoricalOperators, initial=categoricalOperators[0][0], required=True) else: return None
def valueFormField(mymodel, field, widget, allowMultiple=True, label=None, queryGenerator=None): """ Returns form field to provide a value appropriate for this model field """ if isinstance(field, (models.CharField, models.TextField)) or isOrdinalOveridden(mymodel, field): return forms.CharField(required=False,label=label) elif isinstance(field, models.DateTimeField): return forms.DateTimeField(required=False,label=label) elif isinstance(field, models.DateField): return forms.DateField(required=False,label=label) elif isinstance(field, (models.DecimalField, models.FloatField)): return forms.FloatField(required=False,label=label) elif isinstance(field, models.PositiveIntegerField): return forms.IntegerField(min_value=1, required=False,label=label) elif isinstance(field, (models.AutoField, models.IntegerField)): return forms.IntegerField(required=False,label=label) elif isinstance(field, (models.BooleanField, models.NullBooleanField)): return forms.ChoiceField(choices=(("", '<Any>'), (True, True), (False, False)), required=False, label=label) elif isinstance(field, (models.ForeignKey,models.ManyToManyField,models.OneToOneField)): to_field_name = toFieldName(field.rel.to) if queryGenerator: qset = queryGenerator(field.related.model) else: qset = field.related.model.objects.all() if widget is 'pulldown': # can't use as queryset arg because it needs a queryset, not a list # foreigners = sorted(field.related.parent_model.objects.all(), key=lambda x: unicode(x)) if (field.related.model == User): qset = qset.order_by('last_name') if isinstance(field, models.ManyToManyField) and allowMultiple: return forms.ModelMultipleChoiceField(queryset=qset, to_field_name=to_field_name, required=False, label=label) else: return specialModelChoiceField(queryset=qset, # order_by('name'), empty_label="<Any>", required=False, label=label) else: return forms.CharField(required=False,label=label) # return forms.ModelChoiceField(queryset=qset, # to_field_name=to_field_name, # initial=None, # widget=forms.TextInput, # required=False, # label=label) else: return None
def valueFormField(mymodel, field, widget, allowMultiple=True, label=None, queryGenerator=None): """ Returns form field to provide a value appropriate for this model field """ if isinstance(field, (models.CharField, models.TextField)) or isOrdinalOveridden( mymodel, field): return forms.CharField(required=False, label=label) elif isinstance(field, models.DateTimeField): return forms.DateTimeField(required=False, label=label) elif isinstance(field, models.DateField): return forms.DateField(required=False, label=label) elif isinstance(field, (models.DecimalField, models.FloatField)): return forms.FloatField(required=False, label=label) elif isinstance(field, models.PositiveIntegerField): return forms.IntegerField(min_value=1, required=False, label=label) elif isinstance(field, (models.AutoField, models.IntegerField)): return forms.IntegerField(required=False, label=label) elif isinstance(field, (models.BooleanField, models.NullBooleanField)): return forms.ChoiceField(choices=(("", '<Any>'), (True, True), (False, False)), required=False, label=label) elif isinstance( field, (models.ForeignKey, models.ManyToManyField, models.OneToOneField)): to_field_name = toFieldName(field.rel.to) if queryGenerator: qset = queryGenerator(field.related.model) else: qset = field.related.model.objects.all() if widget is 'pulldown': # can't use as queryset arg because it needs a queryset, not a list # foreigners = sorted(field.related.parent_model.objects.all(), key=lambda x: unicode(x)) if (field.related.model == User): qset = qset.order_by('last_name') if isinstance(field, models.ManyToManyField) and allowMultiple: return forms.ModelMultipleChoiceField( queryset=qset, to_field_name=to_field_name, required=False, label=label) else: return specialModelChoiceField( queryset=qset, # order_by('name'), empty_label="<Any>", required=False, label=label) else: return forms.CharField(required=False, label=label) # return forms.ModelChoiceField(queryset=qset, # to_field_name=to_field_name, # initial=None, # widget=forms.TextInput, # required=False, # label=label) else: return None