More About Models In Django-Free Django Tutorials

For basic knowledge about what are the models in django first read this post: https://padhle.com/models-in-django-free-django-tutorials/.

Fields In Django Models:

One of the important and required part of a model is the list of database fields it defines.Fields in django are specified by class attributes.
For example:

from django.db import models

class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    instrument = models.CharField(max_length=100)

class Album(models.Model):
    artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    release_date = models.DateField()
    num_stars = models.IntegerField()

Field Types

Each field in the models of django must be an instance of appropriate field class.For determining some of the following things,django uses the field class types.

  • To determine the column type,Based on the column type database will know what kind of data to store(e.g INTEGERVARCHAR)
  • To determine the minimal validation requirements,which are used in django’s admin and auto-generated forms.

Field options

Each field in the models in django takes a certain set of specific arguments.For example CharField field in the above example requires a max_length argument,which specifies the maximum size of the input we are entering in that field.

There is also a set of common arguments available to all field types in models in django,but all are optional. They’re fully explained in the reference, but here’s a i am writing about some of the most used arguments.

null: If you set this value equal to True,then django will store empty values as NULL in the database. Default  value is False.

blank: If  you set this value equal to True, then the  field is allowed to be blank. Default is False.

Note that blank is different than null. null is purely database-related, whereas  blank is validation-related.

unique: If you set this value equal to True, then this field must be unique throughout the table.

default: This field will set the default value for the field.

primary_key: If you set this value equal to True, then this field is the primary key for the model. For example:

from django.db import models

class Fruit(models.Model):
    name = models.CharField(max_length=100, primary_key=True)

 

>>> fruit = Fruit.objects.create(name='Apple')
>>> fruit.name = 'Pear'
>>> fruit.save()
>>> Fruit.objects.values_list('name', flat=True)
<QuerySet ['Apple', 'Pear']>

Verbose field names

Each of field type in django except for ManyToManyField, OneToOneField and ForeignKey ,takes an optional first positional argument which is known as verbose name.If the verbose name is not given by user,then django will automatically create a verbose name by using the field’s attribute name.For conversion it will remove the underscores to spaces.

For example,in this example the verbose name is "person's first name"

first_name = models.CharField("person's first name", max_length=30)

but in this example user has not specified any verbose name, so django will automatically take the verbose name as "first name"by removing underscore from the field’s attribute name.

first_name = models.CharField(max_length=30)

In the special case of ManyToManyField, OneToOneField and ForeignKey,we will have to add the model class as a first argument instead of verbose name,so use the verbose_name keyword argument as following:

poll = models.ForeignKey(
    Poll,
    on_delete=models.CASCADE,
    verbose_name="the related poll",
)
sites = models.ManyToManyField(Site, verbose_name="list of sites")
place = models.OneToOneField(
    Place,
    on_delete=models.CASCADE,
    verbose_name="related place",
)

So This is all about field types,field options and verbose name in fields in models in django.

Leave a Comment

Your email address will not be published. Required fields are marked *