views:

24

answers:

4

If I want a CharField with only one digit is this the way to write the model

variable = model.CharField(max_length=1)

Or if you only have one Char it is a differnet field?

Thanks!

A: 

If there is only one digit (integer) being used,

You can take a look at:
PositiveSmallIntegerField or SmallIntegerField

Louis
A: 
variable = model.CharField(max_length=1)

This is perfectly fine. But as @Ilian Iliev noted it might be a good idea to tighten it up with choices

CHOICES = (('1', '1'), ..., ('10', '10')) # Simplified; you may want to add more, auto generate this list etc.
variable = model.CharField(max_length=1, choices=CHOICES)

You can also switch to an Integer field

CHOICES = ((1, '1'), ..., (10, '10')) 
variable = model.PositiveIntegerField(choices=CHOICES)
Manoj Govindan
@S.Lott - Because I am a hasty idiot with failing eyesight. Corrected.
Manoj Govindan
@S.Lott - See my first comment. Editing again.
Manoj Govindan
Thanks. What if would like to have multiple Choices on Multiple fields?variable1 = set of choicesvariable2 = set of choicesetc..How do I implement?
MacPython
A: 

If you want to avoid the hideous drop-down selection widget that gets generated for choice fields, you can do this.

Model:

variable = model.CharField(max_length=1)

Form:

def clean_variable(self):
    data = self.cleaned_data['variable']
    if data not in string.digits:
        raise forms.ValidationError("Not a digit")
    return data
S.Lott
Very Interesting. Thanks for that!
MacPython
A: 

Use IntegerField and the clean method that S.Lott show to check is value between 0 and 10

Ilian Iliev