28
loading...
This website collects cookies to deliver better user experience
Model
. We'll understand what the model is, how to structure one, how to create relationships and add constraints on the fields, etc. CREATE TABLE NAME(
attrb1_name type,
attrb2_name type,
.
.
.
);
#from django.db import models
from django.contrib.auth.models import User
class Article(models.Model):
title = models.CharField(max_length=255)
post = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Article')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
from django.db import models
as it is already in the file created by Django. If not, please uncomment the line and that should be good to go.models.py
just append the above code into it. The application can be any application which makes the most sense to you or better create a app if not already created and name it as article
or post
or anything you like.models.Model
class from the django.db
module into our model.sender
, subject
of the mail, body
of the mail, recipients_list
i.e. the To:
section in a mail system and the attachment_file
for a file attachment to a mail if any.#from django.db import models
from user import EmailUser
class EMail(models.Model):
sender = models.EmailField(max_length = 255)
subject = models.CharField(max_length = 78)
body = models.CharField(max_length = 40000)
recipients_list = models.ManyToManyField(EmailUser, related_name = 'mail_list')
attachment_file = models.FileField(blank=True)
ManyToManyField
, what is that? We'll see that shortly.from django.db import models
from user.models import User
class Notes(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length = 1024)
content = models.Textfield()
created = models.DateTimeField(auto_now_add = True)
modified = models.DateTimeField(auto_now = True)
book = models.ManyToManyField(Book, related_name = 'book')
class Book():
name = models.CharField(max_length = 1024)
on_delete
, max_length
, and others in the upcoming section on fields.models
module like name = models.CharField(max_length=10)
, this is a example of defining a attributes name
which is a CharField. We can set the max_length which acts a constraint to the attribute as we do not want the name field to be greater than 10 and hence parsing the parameter max_length
to 10. IntegerField
-> for an integer value.TextField
-> for long input of text (like text area in html).EmailField
-> for an single valid email field.DateField
-> for inputting in a date format. URLField
-> for input a URL field.BooleanField
-> for a boolean value input.ForeignKey
-> Define a many-to-one relationship to another model/class. ManyToManyField
-> define a many-to-many relationship to another model/class.OneToOneField
-> define a one to one relationship between different tables/model/class.null
, blank
, defualt
, choices
, etc. null=True/False
-> Set a check for the entry in the table as not null in the database.blank=True/False
-> Set a check for the input validation to empty or not.unique=True/False
-> Set a constraint to make the entry unique throughout the table.defualt=anyvalue
-> Set a default value for the field.choices=list
-> Set a list of defined choices to select in the field (a list of two valued tuple).max_length
for CharField
, on_delete
for ForeignKey which can be used as a controller for the model when the related model is deleted, verbose_name
to set a different name for referencing the entry in the table/model from the admin section compared to the default name of the model, verbose_name_plural
similar to the verbose_name
but for referencing the entire table/model. Also auto_now_add
and auto_now
for DateTimeField
so as to set the current date-time by default.__str__()
function which is used to rename an object from the database. We also have other predefined helper functions like get_absolute_url
that generates the URL and returns it for further redirection or rendering.#from django.db import models
from django.contrib.auth.models import User
class Article(models.Model):
options = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=255, unique=True)
slug = models.SlugField(max_length=255, unique_for_date='publish')
post = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Posts')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=16, choices=option, default='draft')
def __str__()
return self.title
class Meta:
ordering = ('-publish',)
Draft
and Publish
one which is seen by the django interface and the other to the end-users. We have also added certain fields like slug that will create the URL for the blog post, also certain options like unique
has been set to restrict duplicate entries being posted to the database. The related_name
in the ForeignKey
refers to the name given to the relation from the Article model to the User model in this case. MySQL
, PostgreSQL
, SQLite
, Oracle
, Microsoft Access
, Maria DB
, and tons of others. PostgreSQL
provides the ListField which SQLite
doesn't that can be the decision to be taken before creating any project. There might be some fields that some DBMS provide and other doesn't.