Django zmiana metody w models.py

0

Hejka, utknąłem przy wprowadzaniu metody. Nie wiem gdzie to dopisać

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

do pliku

from django.db import models

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



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

A i jeszcze dokumentacja o co chodzi.
docs.djangoproject.com/pl/2.1/intro/tutorial05/

0

Pomyliłem działy, przepraszam, można przenieść?

0

Widać, że się Zagubiłeś:) tą metode Dodaj pod metodą __str__:

import datetime

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


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)
0

no i dodatkowo myślę, że coś takiego to można dać jako z @property

0

Jednak testy mi nie przechodzą poprawnie i wyskakuje

python manage.py test polls
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/django/mysite/polls/tests.py", line 18, in test_was_published_recently_with_future_question
    self.assertIs(future_question.was_published_recently(), False)
  File "/home/user/django/mysite/polls/models.py", line 10, in was_published_recently
    return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
NameError: name 'timezone' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
Destroying test database for alias 'default'...

a powinno być tak jak w dokumentacji

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Destroying test database for alias 'default'...

Tak wygląda plik tests.py

import datetime

from django.test import TestCase
from django.utils import timezone

from .models import Question


class QuestionModelTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)

0

Interpreter narzeka, że nie ma, w przestrzeni nazw, timezone, Zaimportowałeś?

0

Tak wykonałem zestaw tych poleceń w shell, jednak nie było na końcu True.

python manage.py shell
>>> import datetime
>>> from django.utils import timezone
>>> from polls.models import Question
>>> # create a Question instance with pub_date 30 days in the future
>>> future_question = Question(pub_date=timezone.now() + datetime.timedelta(days=30))
>>> # was it published recently?
>>> future_question.was_published_recently()
True
python manage.py shell
Python 3.7.0 (default, Sep 15 2018, 19:13:07) 
[GCC 8.2.1 20180831] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import datetime
>>> from django.utils import timezone
>>> from polls.models import Question
>>> future_question = Question(pub_date=timezone.now() + datetime.timedelta(days=30))
>>> future_question.was_published_recently()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Question' object has no attribute 'was_published_recently'
>>> 
[1]+  Zatrzymano              python manage.py shell

1 użytkowników online, w tym zalogowanych: 0, gości: 1