reading-notes2

https://m7madmomani2.github.io/reading-notes2

View the Project on GitHub M7madMomani2/reading-notes2

Django Tutorial

Designing the LocalLibrary models

Model definition

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

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

Common field arguments

1) help_text 2) verbose_name 3) default 4) null 5) blank 6) choices 7) primary_key

Common field types

The following list describes some of the more commonly used types of fields.

1) CharField 2) TextField. 3) IntegerField 4) DateField 5) EmailField 6) FileField 7) AutoField 8) ManyToManyField

Advanced configuration

Each model has a list of individual records, identified by the string created with the model’s str() method, and linked to detail views/forms for editing. By default, this view has an action menu at the top that you can use to perform bulk delete operations on records. The model detail record forms for editing and adding records contain all the fields in the model, laid out vertically in their declaration order.