为什么我得到Django .db.models错误告诉我我没有属性的TextChoices在Django(3.1.6)和



感谢您的阅读。我已经在我的电脑上运行了这个程序,没问题。它在我的Windows 10上运行。但是这个错误不断出现,它看起来像TextChoices不再可用?

AttributeError: module 'django.db. .models没有属性TextChoices

我把它放在PythonAnywhere, Python 3.8和Django 3.1.6上我在这方面还是新手,所以请原谅我1

我的问题是与TextChoices,这里是完整的错误:

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'
这是我的models.py在app目录
from django.db import models
from django.utils.timezone import now
from datetime import datetime
# Create your models here.
class Listings(models.Model):
class Country(models.TextChoices):
USA = "USA"
CHINA = "China"
CANADA = "Canada"
INDIA = "India"
UK = "UK"
EUROPE = "Europe"
MIDDLE_EAST = "Middle East"
OTHER = "Other"
class Topic(models.TextChoices):
STATISTICS = "Statistics"
VACCINE = "Vaccine"
NEW_STRAINS = "New Strains"
LOCKDOWN = "Lockdown"
COVID_NEWS = "Covid News"
title = models.CharField(max_length=745)
link = models.CharField(max_length=745)
summary = models.TextField(Null=False)
country = models.CharField(
max_length=100, choices=Country.choices,
default="Country.OTHER")
topic = models.CharField(
max_length=100, choices=Topic.choices,
default="Topic.COVID_NEWS")
list_date = models.DateTimeField(default=now)
photo_1 = models.ImageField()
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

最新更新