Skip to content

Django Models

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

__str__ representation of

why can't we use __repr__

Choices

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

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-09-23