Skip to content

Models

Basics

Models allow us to define tables and its columns

It include two steps:

  • Import models

    from django.db import models
    

  • Define Tables

    • A table is defined as a class and its attributes represents table columns.
    • A simple example

      class MyTable(models.Model):
          id = models.CharField(max_length=12, primary_key=True)
          name = models.CharField(max_length=255, null=False)
      

      Note

      • If we do not provide id and Primary Key is not defined, it automatically created columen id and makes it as a primary key.
      • We access db using attributes defined in our class and not using actual db name.
      • In most of the cases attribute name matches with column name, however there are a few exceptions
        • While using Foreign Keys, _id gets added to column name.
        • We can force db to use specific name using parameter db_column
          class MyTable(models.Model):
              test_id = models.ForeignKey(ForeignModel, null=False, on_delete=models.CASCADE, related_name='test_id', db_column='my_random_column_name')
          

Field Options

For detailed information, refer to

Field Types

Field type can be assigned in the class. In below example we specify field type as foreign key.

class MyTable(models.Model):
    test_id = models.ForeignKey(ForeignModel, null=False, on_delete=models.CASCADE, related_name='test_id', db_column='my_random_column_name')
More
  • null=False

    • If True, Django will store empty values as NULL in the database. Default is False.
  • on_delete=models.CASCADE - ForeignKey.on_delete

    • When an object referenced by a ForeignKey is deleted, Django will emulate the behavior of the SQL constraint specified by the on_delete argument. For example, if you have a nullable ForeignKey and you want it to be set null when the referenced object is deleted.
    • CASCADE deletes. Django emulates the behavior of the SQL constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey.
  • related_name='test_id' - ForeignKey.related_name

    • The name to use for the relation from the related object back to this one.
  • db_column='my_random_column_name' - Field.db_column

    • The name of the database column to use for this field.
    • If this isn’t given, Django will use the field’s name by default.
      • But in case of Foreign Key, django will add _id.
        • That is if you add field role_id and mark it as Foreign Key, it will create column role_id_id in database.
      • So use this parameter to avoid it.

Some common field types are:

Type Reference
url models.URLField(max_length=2048)
foreign key models.ForeignKey(ForeignModel, null=False, on_delete=models.CASCADE, related_name='test_id', db_column='my_random_column_name')
character models.CharField(max_length=512, null=False)
boolean models.BooleanField(default=True)
date time models.DateTimeField(auto_now_add=True)

Note

models.DateTimeField() allows two similar parameters

  • DateField.auto_now

    • Automatically set the field to now every time the object is saved.
    • Useful for “last-modified” timestamps. Note that the current date is always used; it’s not just a default value that you can override.
  • DateField.auto_now_add

    • Automatically set the field to now when the object is first created.
    • Useful for creation of timestamps.
    • Note that the current date is always used; it’s not just a default value that you can override.

For more details, please refer to - https://docs.djangoproject.com/en/3.1/topics/db/models/#field-types

Back to top