Tutorial by Examples

Examples of numeric fields are given: AutoField An auto-incrementing integer generally used for primary keys. from django.db import models class MyModel(models.Model): pk = models.AutoField() Each model gets a primary key field (called id) by default. Therefore, it is not necessary t...
This is a specialized field, used to store binary data. It only accepts bytes. Data is base64 serialized upon storage. As this is storing binary data, this field cannot be used in a filter. from django.db import models class MyModel(models.Model): my_binary_data = models.BinaryField() ...
The CharField is used for storing defined lengths of text. In the example below up to 128 characters of text can be stored in the field. Entering a string longer than this will result in a validation error being raised. from django.db import models class MyModel(models.Model): name = models...
DateTimeField is used to store date time values. class MyModel(models.Model): start_time = models.DateFimeField(null=True, blank=True) created_on = models.DateTimeField(auto_now_add=True) updated_on = models.DateTimeField(auto_now=True) A DateTimeField has two optional parameters:...
ForeignKey field is used to create a many-to-one relationship between models. Not like the most of other fields requires positional arguments. The following example demonstrates the car and owner relation: from django.db import models class Person(models.Model): GENDER_FEMALE = 'F' G...

Page 1 of 1