Skip to content

Django Models

https://betterprogramming.pub/everything-you-need-to-know-about-django-models-in-python-2a44ed4293dd

__str__ : used in template

why can't we use __repr__

Choices (Django 3)

Validators

Querying models (Django ORM)

Django Auth app

Creating models

null=True vs blank=True

  • blank=True can leave it blank for forms
    • example: Django admin form
  • null=True the value in the table can be NULL

Set a foreign key to be something other than an ID

class DashboardWidget(models.Model):
    query = models.ForeignKey(
        DashboardQuery,  # the foreign model
        to_field="file_path",  # DashboardQuery.file_path, must be unique
        db_column="query_file_path",  # name of the db column, default: query_id (field-name_id)
        ...
    )

Doing a migration?

  • you need to drop the DashboardWidget.query and then re-create it
  • AlterField doesn't change the column type from int to text in Django 2.2

Enums in Models

You can also create a subclass for more precision Model Enum Types

class Dashboard(models.Model):
    class Status(models.TextChoices):
        ACTIVE = "active"
        DRAFT = "draft"

    status = models.CharField(
        max_length=6,
        choices=Status.choices,
        default=Status.DRAFT
    )
>>> Dashboard.Status.DRAFT
<Status.DRAFT: 'draft'>

>>> 

There's also a shorthand

MedalType = models.TextChoices('MedalType', 'GOLD SILVER BRONZE')
>>> MedalType.choices
[('GOLD', 'Gold'), ('SILVER', 'Silver'), ('BRONZE', 'Bronze')]

Migrations

Create an empty migration

useful when you're creating a custom migration

python manage.py makemigrations app_name --name migration_name --empty

Create a migration

operations list

from django.db import migrations

class Migration(migrations.Migration):

    dependencies = [
        ('dashboard_etl_manager', '0031_previous'),
    ]

    operations = [
        migrations.RunPython(python_function_name, migrations.RunPython.noop),
        migrations.RunSQL("SELECT * FROM table", migrations.RunSQL.noop),
    ]

Reverse a migration

operations = [
        migrations.RunSQL(
            'CREATE INDEX "app_sale_sold_at_b9438ae4" '
            'ON "app_sale" ("sold_at");',

            reverse_sql='DROP INDEX "app_sale_sold_at_b9438ae4";',
        ),
    ]

Then `python manage.py migrate app_name 0008

Add an index to a table that already has a ton of data in it

Django Admin

You don't even need a ModelAdmin

  • although at Forma, we have a custom ModelAdmin so we shouldn't do this
from django.contrib import admin
from myapp.models import Author

admin.site.register(Author)

Use <input type="text"> instead of <textarea> for TextField

from django.db.models import TextField
from django.forms import TextInput
from django.contrib.admin import ModelAdmin

class PageAdmin(ModelAdmin):

    formfield_overrides = {
        TextField: {"widget": TextInput},
    }

Last update: 2022-11-04