DJANGO FIELD OPTIONS
In this tutorial, we will discuss the following
- What are various field Options in Django ?
- Automatic primary key fields?
- How to understand Field Relationships?
- What are the restrictions while creating the field names?
What are various field Options in Django ?
1.Null:
If True, Django will store empty values as NULL in the database. Default is False.
2. Blank:
If True, the field is allowed to be blank. Default is False.
3. Choices:
A Sequence of 2-tuples to use as choices for this field.
Example:
YEAR_IN_SCHOOL_CHOICES = [
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
]
1. Default:
The default value for the field. This can be a value or a callable object.
2. help_text:
Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.
3. primary_key:
This field is the primary key for the model.
4. Unique
This field must be unique throughout the table.
Automatic primary key fields?
In Django, when we create a model this field is automatically created by default. This field is auto-incrementing primary key.
id = models.AutoField(primary_key=True)For suppose, you want create a custom primary key, then just specify
primary_key=True on one of your fields.So that when you specify it manually then Django won’t add this automatic primary key field. But make it sure that every model contains one field as primary key field.
How to understand Field Relationships?
RDBMS – Relational database management systems are well known for relating the tables to each other. Here, Django is providing the 3 types of database relationships. We generally use these relationships are like field types.
1. many-to-one
2. many-to-many
3. one-to-one
1. Many-to-one relationships
Represent many-to-one relationship using “django.db.models.ForeignKey”. You can use it as just like Field type.
When you use Foreign Key, you should provide an argument that takes the class name to which the model is related.
For example, if a Course model has an Author – that is, multiple Courses created by each Author but each course only has one Author– use the following definitions:
Example:
from django.db import models
class Author(models.Model):
# ...
pass
class Course(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
# ...
2. Many-to-many relationships¶
Represent many-to-many relationship using “django.db.models.ManyToManyField”. You can use it as just like Field type.
When you use ManyToManyField, you should provide an argument that takes the class name to which the model is related.
For example, A Course can be created by multiple Authors, also multiple Courses created by each Author.
from django.db import models
class Author(models.Model):
# ...
pass
class Course(models.Model):
author = models.ManyToManyField(Author)
# ...
3. One-to-one relationships
Represent one-to-one relationship by using “OneToOneField”. This is very useful on the primary key of an object when that object “extends” another object in some way.
When you use “OneToOneField”, you should provide an argument that takes the class name to which the model is related.
What are the restrictions while creating the field names?
There are certain restrictions we need to understand in Django while creating the Field Names.
Django places some restrictions on model field names:
1. A field name should not be a Python reserved word, which result in a Python syntax error.
Example:
class Example(models.Model):
pass = models.IntegerField() # 'pass' is a reserved word!
2. A field name cannot contain more than one underscore in a row, because it clashes with Django’s query lookup syntax.
Example:
class Example(models.Model):
foo__bar = models.IntegerField() # 'foo__bar' has two underscores!
3. A field name should not end with an underscore.