reading-notes

Class 27: Django Models

Using Models

Django web applications access and manage data through Python objects referred to as models.

Models define the structure of stored data, including the field types and possibly also their maximum size, default values, selection list options, help text for documentation, label text for forms, etc.

model is independent of the underlying database

you can choose one of several as part of your project settings

It’s worth taking a few minutes to think about what data we need to store and the relationships between the different objects.

UML Association Diagram

Model primer

Models are usually defined in an application’s models.py file. They are implemented as subclasses of django.db.models.Model, and can include fields, methods and metadata.

from django.db import models

class MyModelName(models.Model):
    """A typical class defining a model, derived from the Model class."""

    # Fields
    my_field_name = models.CharField(max_length=20, help_text='Enter field documentation')
    ...

    # Metadata
    class Meta:
        ordering = ['-my_field_name']

    # Methods
    def get_absolute_url(self):
        """
        Returns the url to access a particular instance of MyModelName."""
        return reverse('model-detail-view', args=[str(self.id)])

    def __str__(self):
        """String for representing the MyModelName object (in Admin site etc.)."""
        return self.my_field_name

Fields

A model can have an arbitrary number of fields, of any type — each one represents a column of data that we want to store in one of our database tables. Each database record (row) will consist of one of each field value.














































Django Admin

Additional Resources

Read (Optional): Beginner’s Guide to Django - Part 1

Beginner’s Guide to Django - Part 2