Wednesday, May 12, 2021

Re: 'Question' object has no attribute 'choice_text'

Hi Daniel,

Thank you for getting back to me.

Please find models as follows

Need all_Questions and all_Choices to reflect for each other.

I was reading MDN docs on django they recommend ManyToManyField. 

Please advise.

Thank You
Rishipal

import datetime

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

# Create your models here.

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):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now

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

On Fri, 7 May 2021 at 19:22, Daniel Hepper <daniel.hepper@gmail.com> wrote:
Hi Rishi,

please share your models.py, otherwise it will be difficult to help you. 

Cheers,
Daniel

Rishipal Singh <acc.europeous.uk@gmail.com> schrieb am Fr. 7. Mai 2021 um 15:19:
Hi Daniel, 
I am working on same tutorial and stuck at Tutorial # 4..

I have checked your code too.. 
Still getting below error

'Question' object has no attribute 'choice_set'
 selected_choice = question.choice_set.get(pk=request.POST['choice'])
Can you advice

Regards
Rishi
On Monday, 25 December 2017 at 05:22:55 UTC+5:30 Daniel Hepper wrote:
Are you sure your polls/models.py is correct?
I assume you are at „Part 2: Playing with the API". Here is what your polls/models.py should look like at this point of the tutorial:

My guess is that you have a typo in your __str__ method. This gets triggered in the first session because your Question had Choices, which it doesn't in your second session, maybe because you deleted them. But that's really just a guess without looking at your code.

I've put together the complete code for the tutorial, step-by-step:

Hope that helps,
Daniel

Am 24.12.2017 um 17:44 schrieb Artem Tantsura <duali...@gmail.com>:

I've made anything by site tutorial, just repeat all steps and code. And when I wrote this:

 >>> from polls.models import Question, Choice
# Make sure our __str__() addition worked.  >>> Question.objects.all()  <QuerySet [<Question: What's up?>]>    # Django provides a rich database lookup API that's entirely driven by  # keyword arguments.  >>> 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):      ...  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()

Im getting 'Question' object has no attribute 'choice_text'. 
But then I have got some magic solution! I define if Im writting only this:
>>>from polls.models import Choice, Question  >>>from django.utils import timezone    >>>q = Question.objects.get(pk=1)    >>>q.choice_set.all()  Out[4]: <QuerySet []>  
Then I dont have any problems, but I have done the same main steps then before(like in tutorial)! Sorry, but WTF is going on?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/33e82e65-b97a-46f9-be2c-38905fccc01a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f8978213-d64c-412e-b65c-d55f1bd93db7n%40googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/e0h4r-t8ypE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHEnUVVpmGvfNa%2B5_oVvmifmyy%2BSNiK_SQsDcYy3H1SnwoHXtQ%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANvCymPfnU-6idK1%2BcSBLQBGx1A03MVwSy7yo17Gx23xxr%3DmFw%40mail.gmail.com.

No comments:

Post a Comment