Django models are used to represent the data in a database and are defined as Python classes. Each field in a model represents a column in the corresponding database table.
The following is a list of the most commonly used fields in Django models, along with their required and optional arguments:
CharField:
It represents a character field in the database.
Required arguments: max_length
.
Optional arguments: choices
, default
, blank
, null
.
class Post(models.Model):
title=models.CharField(max_length=255)
IntegerField:
Represents an integer field in the database.
Required arguments: None
.
Optional arguments: choices
, default
, blank
, null
.
class Post(models.Model):
# other fields
id = models.IntgerField()
DateField:
Represents a date field in the database.
Required arguments: None
.
Optional arguments: auto_now
, auto_now_add
, blank
, null
.
class Post(models.Model):
# other fields
created_on = models.DateField(auto_now_add=True) # when object of model will create this will execute
updated_at = models.DateTimeField(auto_now=True) # when object of model is updated this will execute
# DateTimeField is also avalaible
BooleanField:
Represents a boolean field in the database.
Required arguments: None
.
Optional arguments: default
, blank
, null
.
class Post(models.Model):
# other fields
status = models.BoolenField()
TextField:
Represents a Textarea field in the HTML.
Required arguments: None
.
Optional arguments: default
, blank
, null
.
class Post(models.Model):
# other fields
content = models.TextField()
EmailField:
Represents an email field in the database.
Required arguments: None
.
Optional arguments: max_length
, default
, blank
, null
.
class Post(models.Model):
# other fields
email = models.EmailField()
URLField:
Represents a URL field in the database.
Required arguments: None
.
Optional arguments: max_length
, default
, blank
, null
.
class Post(models.Model):
# other fields
site = models.URLField()
ForeignKey:
Represents a foreign key field in the database.
Required arguments: Model name
.
Optional arguments: on_delete
, blank
, null
.
class Post(models.Model):
# other fields
user = models.ForeignKey('User')
ManyToManyField:
Represents a many-to-many relationship field in the database.
Required arguments: model name
.
Optional arguments: blank
, null
.
class Post(models.Model):
# other fields
tags = models.ManyToManyField('Tag')
The use of these fields varies depending on the specific application and the data that needs to be stored in the database. For example, CharField is commonly used to store short strings of text, such as a name or a title, while IntegerField is used to store numerical values. The ForeignKey and ManyToManyField fields are used to define relationships between different models, while the DateField and EmailField fields are used to store specific types of data.
Overall, fields are crucial for the functioning of a Django model. They define the structure of the database and the data that it will hold. They also help in validating the data provided by the user.
I hope this tutorial has helped you to understand Fields in the Django models.
Thanks for reading 🤩 and follow me on Twitter ❤️.