Choices can be any sequence object – not necessarily a list or tuple. The first element in each tuple is the actual value to be set on the model, and the second element is the human-readable name. Let us create a choices field with above semester in our django project named geeksforgeeks.
Let's try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field. Hit submit. Hence Field is accepting the form even without any data in the geeks_field. This makes required=False implemented successfully.
IntegerField is a integer number represented in Python by a int instance. This field is generally used to store integer numbers in the database. The default form widget for this field is a NumberInput when localize is False or TextInput otherwise.
Looking at the source for django...
class CommaSeparatedIntegerField(CharField):
def formfield(self, **kwargs):
defaults = {
'form_class': forms.RegexField,
'regex': '^[\d,]+$',
'max_length': self.max_length,
'error_messages': {
'invalid': _(u'Enter only digits separated by commas.'),
}
}
defaults.update(kwargs)
return super(CommaSeparatedIntegerField, self).formfield(**defaults)
Check out that regex validator. Looks like as long as you give it a list of integers and commas, django won't complain.
You can define it just like a charfield basically:
class Foo(models.Model):
int_list = models.CommaSeparatedIntegerField(max_length=200)
And populate it like this:
f = Foo(int_list="1,2,3,4,5")
I would like to add that this Field is depricated in Django 1.9. In Django 1.9+ a CharField
with validators=[validate_comma_separated_integer_list]
should be used.
Works for versions greater than Django 1.9
First Step:- Import this ( May change in newer versions , so double check that)
from django.core.validators import validate_comma_separated_integer_list
Second Step :- Write Field
class RestaurantLocation(models.Model):
name = models.CharField(max_length=200)
location = models.CharField(max_length=200,null=True,blank=True)
category = models.CharField(max_length=200,null=True,blank=False)
choices_field = models.CharField(validators=[validate_comma_separated_integer_list],max_length=200, blank=True, null=True,default='')
def __str__(self):
return self.name
Note: Please make sure to use default = ''
if you are adding column to an already created database otherwise you would get the following option to choose after running python manage.py migrate
You are trying to add a non-nullable field 'choices_field' to restaurantlocation without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a
null value for this column)
2) Quit, and let me add a default in models.py
Select an option:
Happy Coding!
I just happened to have dealt with CommaSeparatedIntegerField in my latest project. I was using MySQL and it seemed like supplying a string of comma separated integer values is very DB friendly e.g. '1,2,3,4,5'. If you want to leave it blank, just pass an empty string.
It does act like a CharField, and beaved in a weird way for me.. I ended up with values like "[1, 2, 3, 4, 5]" including the brackets in my database! So watch out!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With