Hello,
I’m doing my first steps in django and I’m trying to translate this sql into a view.
this is the 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```
this is the sql:
```select DISTINCT question_text from polls_choice c, polls_question q
where c.question_id = q.id```
this is what i've got in the view:
```def index(request):
latest_question_list = Question.objects.all().distinct(Question).order_by('-pub_date')
context = {'latest_question_list': latest_question_list,}
return render(request, 'polls/index.html', context)```
I'm getting this error:
```# AttributeError at /polls/
type object 'Question' has no attribute 'split'
|Request Method:|GET|
| --- | --- |
|Request URL:|http://127.0.0.1:8000/polls/|
|Django Version:|4.1.3|
|Exception Type:|AttributeError|
|Exception Value:|type object 'Question' has no attribute 'split'|
|Exception Location:|/Users/demiguelfreitas/develop-env/lib/python3.8/site-packages/django/db/models/sql/compiler.py, line 863, in get_distinct|
|Raised during:|polls.views.index|
|Python Executable:|/Users/demiguelfreitas/develop-env/bin/python3|
|Python Version:|3.8.1|
|Python Path:|['/Users/demiguelfreitas/develop-env/luproject', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload', '/Users/demiguelfreitas/develop-env/lib/python3.8/site-packages']|
|Server time:|Tue, 22 Nov 2022 18:00:47 +0000|```
this is the index.html template:
```{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available</p>
{% endif %}```
can someone help on this?
Thanks in advance