Works pour les versions supérieures à Django 1,9
Première étape: -Importer cette (peut changer dans les versions plus récentes, donc vérifiez que)
from django.core.validators import validate_comma_separated_integer_list
deuxième Étape: -Champ d'écriture
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: S'il vous plaît assurez-vous d'utiliser default = ''
si vous ajoutez la colonne à une base de données déjà créée, sinon vous obtiendrez l'option suivante pour choisir après l'exécution 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:
Bonne programmation !
Savez-vous si le paramètre max_length correspondant au champ contient des virgules? – khalid13
oui, il comprend comme c'est une chaîne. –