Models

Unlike Ruby/Phoenix, migrations are entirely derived from the models

Using Models

Importing

from polls.models import Choice, Question

Querying

Question.objects.all() #<QuerySet []>
Question.objects.filter(id=1)
Question.objects.filter(question_text__startswith='What')

# Identical
Question.objects.get(pk=1)
Question.objects.get(id=1)

Question.objects.get(pub_date__year=timezone.now().year) #Will throw error if doesn't exist

Querying through relations

Use two underscores to seperate relationships with unlimited layers

Creation

Updating

Children

If choice is a child of q

Delete

Creating New Model

  • Table names will be app and model in lowercase e.g polls_question

  • Columns will be field e.g question_text

Migrations

After making changes to the model file, you need to create the migrations with:

Then actually run the migrations

Last updated