Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

python - Field Error When Using choices in a Django Model field

After adding one of the model fields to select an option from a list, I get a field error. What could possibly be the cause and the solution to this

ERROR:

  File "/home/chrisdev/code/work/cw-full/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1481, in names_to_path
    raise FieldError("Cannot resolve keyword '%s' into field. "
django.core.exceptions.FieldError: Cannot resolve keyword 'status' into field. Choices are: description, id, image, name, slug

MODEL:

from django.db import models
from django.contrib.auth.models import User

# News Model
class News(models.Model):
    DRAFT = 'DRT'
    PUBLISHED = 'PUB'
    article_publication_choices = [
        (DRAFT, 'Draft'),
        (PUBLISHED, 'Published'),
    ]
    title = models.CharField('News Title', max_length=200, unique=True, help_text='News Heading')
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(User, on_delete= models.SET_NULL, related_name='news', null=True)
    updated_on = models.DateTimeField(auto_now= True)
    news_summary = models.CharField('News Summary', max_length=200)
    content = models.TextField('News Content')
    created_on = models.DateTimeField(auto_now_add=True)
    article_publication = models.CharField(max_length=2,
        choices=article_publication_choices,
        default=PUBLISHED,
    )

    class Meta:
        verbose_name = 'News Updates'
        verbose_name_plural = verbose_name
        ordering = ['-created_on']

    def __str__(self):
        return self.title
question from:https://stackoverflow.com/questions/65874348/field-error-when-using-choices-in-a-django-model-field

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the solution to this, by adding related_name='+', at the field that uses a relationship from other models


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...