为什么我在django(3.1.6)和Python(3.8)中得到django.db.models错误,告诉我没有TextChoices的属性?
非常感谢你的阅读。我已经在我的电脑上运行了,没问题。它正在Windows 10上运行我的VS。但是这个错误不断出现,看起来TextChoices不再可用了 AttributeError:模块“django.db.models”没有属性“TextChoices” 我将它放在PythonyWhere、Python3.8和Django 3.1.6上 我对这一点还不熟悉,所以请原谅我 我的问题是TextChoices,下面是完整的错误:为什么我在django(3.1.6)和Python(3.8)中得到django.db.models错误,告诉我没有TextChoices的属性?,python,django,forms,django-forms,Python,Django,Forms,Django Forms,非常感谢你的阅读。我已经在我的电脑上运行了,没问题。它正在Windows 10上运行我的VS。但是这个错误不断出现,看起来TextChoices不再可用了 AttributeError:模块“django.db.models”没有属性“TextChoices” 我将它放在PythonyWhere、Python3.8和Django 3.1.6上 我对这一点还不熟悉,所以请原谅我 我的问题是TextChoices,下面是完整的错误: Traceback (most recent call last)
Traceback (most recent call last):
File "/home/sandorf/project/listings/models.py", line 6, in <module>
class Listings(models.Model):
File "/home/sandorf/project/listings/models.py", line 7, in Listings
class Country(models.TextChoices):
AttributeError: module 'django.db.models' has no attribute 'TextChoices'
您可以这样使用:
from django.db import models
from django.utils.timezone import now
from datetime import datetime
# Create your models here.
class Listings(models.Model):
COUNTRIES = (
('US', 'United States of America'),
('CH', 'China'),
)
title = models.CharField(max_length=745)
country = models.CharField(max_length=50, choices=COUNTRIES, default='US')
def __str__(self):
return self.title
谢谢你,霍夫曼,第一次尝试就成功了。非常感谢!
from django.db import models
from django.utils.timezone import now
from datetime import datetime
# Create your models here.
class Listings(models.Model):
COUNTRIES = (
('US', 'United States of America'),
('CH', 'China'),
)
title = models.CharField(max_length=745)
country = models.CharField(max_length=50, choices=COUNTRIES, default='US')
def __str__(self):
return self.title