Hi to all
Hopefully you can help me on my struggle in following the Part 2 Django tutorial. Here’s the code
from datetime import timedelta
from django.utils import timezone
from django.db import models
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
return self.pub_date >= (timezone.now() - timedelta(days=1))
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default = 0)
after all the code is written and typing in my command line q.was_published_recenlty()
I got the following result:
File “console”, line 1, in module
File “C:\User… \models.py”, line 12, in was_published_recently…
Name Error:name ‘pub_date’ is not defined
Hi, did you create and run migration?
I follow what was written in this tutrial (https://docs.djangoproject.com/en/3.0/intro/tutorial02/)
and after typing the command line of q.was_published_recently()
and then I got the error message.
What am I lacking?
Go back and rerun the steps from here: https://docs.djangoproject.com/en/3.0/intro/tutorial02/#activating-models
The “makemigrations” step should show something like “no changes detected”, which is ok.
What’s more important is that the sqlmigrate command generates output like what the tutorial shows.
Then run the migrate command after that. Again, it will either show the output as shown or “No migrations to apply”.
If it does work as displayed, exit the shell, re-enter the shell, then retry the commands you were entering in the shell when the error message appeared.
If you’re still having problems, copy the text of the commands and output from the commands above as well as the text from your shell session with the statements you’re entering.
Thnks for the prompt reply sir KenWhitesell,
after re-doing the Part 2 tutorial. I’m lost with this trace back
I’m in the code portion of-
Get the question that was published this year.
>>> from django.utils import timezone
>>> current_year = timezone.now().year
<Question: What's up?>
and I got Trace back
File "<console>", line 1, in <module>
File "C:\Users\Racho\myproject\lib\site-packages\django\db\models\manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\Users\Racho\myproject\lib\site-packages\django\db\models\query.py", line 419, in get raise self.model.MultipleObjectsReturned( polls.models.Question.MultipleObjectsReturned: get() returned more than one Question -- it returned 2!
The key is the last line of the traceback -
polls.models.Question.MultipleObjectsReturned: get() returned more than one Question -- it returned 2!
You’re using the
get method, so you probably want to review the documentation on get.
Then review what you have done previously, and think about what might have happened to create this situation.
use filter() insted of get()