ruk·si

Django Tutorial
Adding Models

Updated at 2018-11-23 16:21

Add polls/models/questions.py:

from django.conf import settings
from django.db import models

from djangor.model_mixins import UUIDMixin, TimesMixin


class Question(UUIDMixin, TimesMixin, models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
    text = models.CharField(max_length=200)

Add polls/models/choices.py:

from django.db import models

from djangor.model_mixins import UUIDMixin, TimesMixin
from .questions import Question


class Choice(UUIDMixin, TimesMixin, models.Model):
    question = models.ForeignKey(
        Question,
        on_delete=models.CASCADE,
        related_name='choices'
    )
    text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

Add polls/models/__init__.py:

from .choices import Choice
from .questions import Question

Edit djangor/settings.py:

INSTALLED_APPS = [
    # ...
    'polls.apps.PollsConfig',
]
python manage.py makemigrations
python manage.py migrate
# if you need to rollback everything...
# python manage.py migrate polls zero