https://m7madmomani2.github.io/reading-notes2
When designing your models it makes sense to have separate models for every “object” (a group of related information). In this case, the obvious objects are books, book instances, and authors.
We might need to store more information about the author than just their name, and there might be multiple authors with the same or similar names. We want to be able to sort information based on book title, author, written language, and category.
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
my_field_name = models.CharField(max_length=20, help_text='Enter field documentation')
1) help_text 2) verbose_name 3) default 4) null 5) blank 6) choices 7) primary_key
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
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.