Skillversitas

Models and the ORM

The field is the full: the CharField (the varchar, the max_length), the TextField (the text), the IntegerField (the int), the BooleanField (the bool), the DateTimeField (the timestamp, the auto_now_add), the FloatField (the decimal), the DecimalField (the money, the no the float), and the ForeignKey (the M2M, the OneToOne) the relation. The related_name is the access (the note.tags the M2M, the post.author the FK), and the on_delete is the rule (the CASCADE, the SET_NULL, the PROTECT). This chapter covers the field (the 8 the type), the relation (the FK, the M2M, the O2O, the related_name), the on_delete (the 4 the rule), and the clean the save the signal the override.

The fields: The 8 type

# the FIELD (the 8, the type's the full):
from django.db import models

class Post(models.Model):
    title = models.CharField(
        max_length=200)      #   the varchar
    slug = models.SlugField(
        unique=True)         #   the URL's
    body = models.TextField()   #   the text
    views = models.IntegerField(
        default=0)           #   the int
    published = models.BooleanField(
        default=False)       #   the bool
    price = models.DecimalField(
        max_digits=10,
        decimal_places=2)    #   the money
    created = models.DateTimeField(
        auto_now_add=True)   #   the ts
    updated = models.DateTimeField(
        auto_now=True)       #   the touch
#   the the 3: the max_length (the
#     the CharField, the no the
#     the TextField), the default (the
#     the no the null), the unique
#     (the index, the the 1)

The field is the 8: the CharField (the max_length the required, the varchar), the SlugField (the unique, the URL), the TextField (the no the max), the IntegerField (the default the 0), the BooleanField (the default the False), the DecimalField (the money, the 10 the 2, the no the float the round), the DateTimeField (the auto_now_add the 1, the auto_now the every the save), and the ImageField (the upload_to, the Pillow). The null=True is the no (the blank=True the form, the null the DB, the blank the validation - the 2 the separate), and the choices is the enum (the (DRAFT, "Draft"), the form the select the no the query) - the relation is the next section: the FK the M2M the O2O, the related_name the access, the on_delete the rule.

The relation and the on_delete

# the RELATION (the FK, the M2M, the O2O):
class Author(models.Model):
    name = models.CharField(max_length=100)

class Post(models.Model):
    title = models.CharField(max_length=200)
    #   the FK (the 1 author the many
    #     post, the CASCADE):
    author = models.ForeignKey(
        Author, on_delete=models.CASCADE,
        related_name="posts")
    #   the M2M (the 1 post the many
    #     tag, the no the through):
    tags = models.ManyToManyField(
        Tag, related_name="posts",
        blank=True)
    #   the O2O (the 1 profile the 1
    #     user, the no the null):
    profile = models.OneToOneField(
        Author, on_delete=models.PROTECT)
#   the ACCESS (the related_name):
#   the 1: the author.posts.all()
#   the 2: the post.tags.all()
#   the 3: the post.profile.name
#   the 4: the post.author.name (the
#     the FK's the forward)

# the ON_DELETE (the 4, the rule's):
#   the CASCADE (the 1 the no, the
#     the 1 the delete, the default)
#   the SET_NULL (the author the no,
#     the post the keep, the null)
#   the PROTECT (the author the no,
#     the error, the the 1 the no)
#   the SET_DEFAULT (the author the no,
#     the post the keep, the default)

The relation is the 3: the ForeignKey (the 1 author the many post, the author_id the column, the post.author the forward), the ManyToManyField (the 1 post the many tag, the through table the auto, the post.tags the access), and the OneToOneField (the 1 profile the 1 author, the unique the FK) - the related_name is the back (the author.posts, the tag.posts, the no the related_name the post_set). The on_delete is the 4: the CASCADE (the 1 the delete the many, the default), the SET_NULL (the 1 the no, the many the keep, the null=True the required), the PROTECT (the 1 the no, the error, the the the no), and the SET_DEFAULT (the 1 the no, the many the default) - the clean is the validate (the full_clean the 1 the no the save, the form the use), the save is the override (the slug the auto, the super().save), and the signal is the hook (the pre_save, the post_save, the no the magic) - the next chapter's migration is the table the schema the no the SQL.

The Working Rules

  • The field is the 8, the max_length the CharField. The CharField the varchar (the max_length), the TextField the no, the IntegerField the default, and the DecimalField the money (the no the float) - the auto_now_add the 1, the auto_now the every save, the null the DB, and the blank the form (the 2 the separate).
  • The ForeignKey is the 1 the many, the CASCADE the default. The author_id the column, the post.author the forward, and the author.posts the back (the related_name, the no the post_set) - the M2M the through the auto (the post.tags), and the OneToOne the unique (the profile the 1 the 1).
  • The on_delete is the 4, the CASCADE the no. The CASCADE the 1 the delete the many, the SET_NULL the keep the null=True, the PROTECT the error the no, and the SET_DEFAULT the keep the default - the PROTECT the no the delete the referenced, the SET_NULL the history the keep, and the CASCADE the 1 the no the many the no.
  • The clean is the validate, the save the override. The full_clean the 1 the no the save (the form the use, the manual the no), and the save the slug the auto (the super().save() the call) - the signal the pre_save the post_save (the no the magic, the explicit the prefer), and the property the computed (the no the field, the no the store).

Summary

The field is the 8: the CharField (the max_length, the varchar), the TextField (the no the max), the IntegerField (the default), the BooleanField (the default the False), the DecimalField (the money, the no the float), the DateTimeField (the auto_now_add the 1, the auto_now the every), the SlugField (the unique, the URL), and the ImageField (the upload_to, the Pillow). The relation is the 3: the ForeignKey (the 1 the many, the author_id, the post.author the forward, the author.posts the back), the ManyToManyField (the through the auto, the post.tags), and the OneToOneField (the unique, the profile). The on_delete is the 4: the CASCADE (the default, the 1 the no the many), the SET_NULL (the keep, the null=True), the PROTECT (the error, the no), and the SET_DEFAULT (the keep, the default). The clean is the validate (the full_clean the no the save), the save is the override (the slug the auto), and the signal is the hook (the pre_save, the post_save) - the next chapter's migration is the table the schema: the makemigrations the diff, the migrate the SQL, the no the hand the write.