Django Interactive Shell API Error (2)

Asked 2 years ago, Updated 2 years ago, 433 views

Prerequisites/What you want to achieve
I am working on a tutorial on Django.
I was referring to the following site to use the command to call the interactive shell and access the database API, but an error occurred:Please tell me the solution.
https://docs.djangoproject.com/ja/4.0/intro/tutorial02/

Problems/Error Messages you are experiencing

>>>from polls.models import Choice, Question
>>Question.objects.all()
<QuerySet [<Question: What's up?>]>
>>Question.objects.filter(id=1)
<QuerySet [<Question: What's up?>]>
>>Question.objects.filter(question_text__startswith='What')
<QuerySet [<Question: What's up?>]>
>>> from django.utils import timezone
>>current_year=timezone.now().year
>>Question.objects.get(pub_date__year=current_year)
<Question: What's up?>
>>
>>Question.objects.get(id=2)
Traceback (most recent call last):
  "<console>", line 1, in <module>
  "\anaconda3\envs\ev1\lib\site-packages\django\db\models\manager.py", line85, inmanager_method
    return getattr(self.get_queryset(),name)(*args,**kwargs)
  "\anaconda3\envs\ev1\lib\site-packages\django\db\models\query.py", line 496, inget
    raise self.model.DoesNotExist(
pols.models.Question.DoesNotExist—Question matching query does not exist.
>>Question.objects.get(pk=1)
<Question: What's up?>
>>>q=Question.objects.get(pk=1)
>>q.was_published_recently()
True
>>>q=Question.objects.get(pk=1)
>>q.choice_set.all()
<QuerySet[]>
>>q.choice_set.create(choice_text='Not much',votes=0)
<Choice: Not much>
>>q.choice_set.create(choice_text='The sky',votes=0)
<Choice: The sky>
>>>c=q.choice_set.create(choice_text='Just hacking again',votes=0)
>>>c.question
<Question: What's up?>
>>q.choice_set.all()
<QuerySet[<Choice:Not much>,<Choice:The sky>,<Choice:Just hacking again>]>
>>q.choice_set.count()
3
>>Choice.objects.filter(question_pub_date_year=current_year)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'current_year' is not defined

Affected Source Code
pols /models.py

import datetime
from django.utils import timezone
from django.db import models

class Question (models.Model):
    question_text=models.CharField(max_length=200)
    pub_date=models.DateTimeField('date published')

    def__str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date>=timezone.now()-datetime.timedelta(days=1)

Class Choice (models.Model):
    question=models.ForeignKey (Question, on_delete=models.CASCADE)
    choice_text=models.CharField(max_length=200)
    votes=models.IntegerField(default=0)

    def__str__(self):
        return self.choice_text

python django

2022-09-30 22:04

1 Answers

Comment Ready

I'm sorry, but I answered reflexively whether or not there was indentation at the beginning of the question.
In conclusion, the error is the correct situation as Tutorial

The Tutorial article corresponds to the following parts:

>>Question.objects.get(id=2)
Traceback (most recent call last):
    ...
DoesNotExist—Question matching query does not exist.

In my environment, it appears as follows:

>>Question.objects.get(id=2)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Develop\Python\vDjango\lib\site-packages\django\db\models\manager.py", line85, in manager_method
    return getattr(self.get_queryset(),name)(*args,**kwargs)
  File "C:\Develop\Python\vDjango\lib\site-packages\django\db\models\query.py", line 496, inget
    raise self.model.DoesNotExist(
pols.models.Question.DoesNotExist—Question matching query does not exist.

So don't worry about continuing to do the following:

Incidentally, the source code for the updated question still lacks indentation in the return line.
You should fix it.

Below is a slightly off-target answer to the source code of the original question.

The indentation of the method part is insufficient.
Please do the following:

import datetime
from django.utils import timezone
from django.db import models

class Question (models.Model):
    question_text=models.CharField(max_length=200)
    pub_date=models.DateTimeField('date published')
    def__str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date>=timezone.now()-datetime.timedelta(days=1)

Class Choice (models.Model):
    question=models.ForeignKey (Question, on_delete=models.CASCADE)
    choice_text=models.CharField(max_length=200)
    votes=models.IntegerField(default=0)

    def__str__(self):
        return self.choice_text

You're working on a Django tutorial, but you'll need some basic knowledge of Python before you do that.

In parallel with the Django tutorial, we recommend that you also look for course materials on Python's basic knowledge.


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.