Works für Versionen größer als Django 1,9
Erster Schritt: -Import dieses (Mai in neueren Versionen ändern, so überprüft, dass)
from django.core.validators import validate_comma_separated_integer_list
Zweite Schritt: -Feld
schreiben
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
Hinweis: Bitte stellen Sie sicher, default = ''
zu verwenden, wenn Sie Spalte auf eine bereits erstellte Datenbank hinzugefügt werden sonst würden Sie die folgende Option erhalten, nachdem 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:
Glücklich zu wählen laufen Coding !
Wissen Sie, ob die dem Feld entsprechende max_length Kommata enthält? – khalid13
ja, es enthält, da es eine Zeichenfolge ist. –