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
- Django apps uses models to access and manage data
- Models basically refers to the database, the tables of data/information about objects.
- one to one (OneToOneField)
- one to many (ForeignKey)
- many to many (ManyToManyField)
- model is Djngo are defined in models.py file, in created app.
-
from django.db import models
- Fields: Data attribute - column
-
my_field_name = models.CharField(max_length=20, help_text='Enter field documentation')
- Common field arguments:
- primary_key
- help_text
- default
- choices
-
- Metadata
- Data about models
- Meta data are defined throug :
class Meta: ordering = ['-my_field_name']
- Methods
-
minimally you should include str method in model, makes data readable for humans.
-
def str(self): return self.field_name
-
-
get_absolute_url(), is another common method
-
which returns a URL for displaying individual model records on the website (if you define this method then Django will automatically add a "View on Site" button to the model's record editing screens in the Admin site).
-
def get_absolute_url(self): """Returns the url to access a particular instance of the model.""" return reverse('model-detail-view', args=[str(self.id)])
-
-
- Managing model basically includes, updating, or deleting records, and running select queries.
- Create a new record using the model's constructor. record = MyModelName(my_field_name="Instance #1")
- Save the object into the database.
record.save()
- search is done using attributes of the object
- Getting All Records
- QuerySet --> objects.all()
- Getting Specific Records Based on a Criteria
- QuerySet --> filter()
from django.db import models from django.urls import reverse import uuid