thanks so much for reading. I’ve got this running on my computer no problem. It’s working on my VS on Windows 10. But this error keeps coming up, it looks like TextChoices is no longer usable?
AttributeError: module ‘django.db.models’ has no attribute ‘TextChoices’
I’m putting it u0p on PythonAnywhere, Python 3.8, and Django 3.1.6 I am still new at this, so please forgive me1
My issue is with the TextChoices, here is the full error:
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'
This is my models.py in the app directory
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
Advertisement
Answer
You can use choices this way:
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