Tutorial problem

I want to try out out Django and on my Linux Mint 19 PC have installed python 3.6.9 and Django 3.0 and am using the built-in SQLite3 for testing purposes although I do have MySQL installed on the PC.
I am trying to do the tutorial but have twice come a cropper at the step in Part 2 of the tutorial where String Methods are added to polls/models/py. When in the shell, testing out if adding the methods has worked, in response to

Question.objects.all()
I get
<QuerySet [<Question: Question object (1)>]>
instead of the expected:
<QuerySet [<Question: What’s up?>]>
All the preceding steps in the tutorial worked without a hitch. It is only when trying to add the def str(self): part (copied and pasted from the tutorial) that things don’t go according to plan.
I am also unsure as to whether the “from django.db import models” line should be included again in the models.py file, since the very fist line is identical.
Any help will be much appreciated.

Hi @pnewbery

That step in the tutorial is telling you to add the __str__ functions to the existing code. It sounds to me like you’ve instead understood it as copying the new code at the end of the models.py file?

You should have just this in your models.py after adding those methods:

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


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

Thanks for that. Now it begins to make sense. However…
Things get even more muddled. If I try to do:
python3 manage.py makemigrations polls
I get
You are trying to add a non-nullable field ‘choice_text’ to choice without a default; we can’t do that (the database needs something to populate existing rows).
Please select a fix:

  1. Provide a one-off default now (will be set on all existing rows with a null value for this column)
  2. Quit, and let me add a default in models.py
    Select an option:

I don’t know what to do here.

If I run the shell, the response to:
Question.objects.all() is now:
Traceback (most recent call last):
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/backends/utils.py”, line 86, in _execute
return self.cursor.execute(sql, params)
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py”, line 396, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: polls_question.question_text

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/usr/lib/python3.6/code.py”, line 91, in runcode
exec(code, self.locals)
File “”, line 1, in
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/models/query.py”, line 252, in repr
data = list(self[:REPR_OUTPUT_SIZE + 1])
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/models/query.py”, line 276, in iter
self._fetch_all()
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/models/query.py”, line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/models/query.py”, line 57, in iter
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py”, line 1137, in execute_sql
cursor.execute(sql, params)
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/backends/utils.py”, line 100, in execute
return super().execute(sql, params)
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/backends/utils.py”, line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/backends/utils.py”, line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/backends/utils.py”, line 86, in _execute
return self.cursor.execute(sql, params)
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/utils.py”, line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/backends/utils.py”, line 86, in _execute
return self.cursor.execute(sql, params)
File “/home/edwin/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py”, line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: polls_question.question_text

Oops!!

OK, it’s sorted! I started the project all over again and now that I understand the structure of models.py, it’s behaving as it should. My thanks to @adamchainz for putting me straight on this.