trình cho các phiên bản lớn hơn Django 1,9
Bước đầu tiên: -nhập khẩu (Có thể thay đổi trong các phiên bản mới hơn, kiểm tra để gấp đôi) này
from django.core.validators import validate_comma_separated_integer_list
Second Bước: -Viết trường
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
Lưu ý: Hãy chắc chắn để sử dụng default = ''
nếu bạn đang thêm cột vào một cơ sở dữ liệu đã được tạo ra nếu không bạn sẽ nhận được các tùy chọn sau đây để lựa chọn sau khi chạy 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:
Chúc mừng Mã hóa !
Nguồn
2018-02-26 14:00:51
Bạn có biết liệu max_length tương ứng với trường có dấu phẩy không? – khalid13
có, nó bao gồm vì nó là một chuỗi. –