1 - About the Django models 

In order to organize the storage data for your app, django offers you the tool called model (django model).

  1. A model shelters in the form of classes the names of the tables and fields (attributes) of the storage data. 
  2. Each model corresponds to a single database table. 
  3. Each model is a Python class that inherits from django.db.models.Model. 
  4. Each attribute of the model represents a database field. 

2 - The different types of fields in a django model

Django template provides a variety of field types:

  1. AutoField: designates an integer field which increments automatically. 
  2. BigAutoField: this is a 64-bit integer, much like an AutoField, except that it is guaranteed that it can contain numbers from 1 to 9223372036854775807. 
  3. BigIntegerField: this is a 64-bit integer, much like an IntegerField, except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. 
  4. BinaryField: a field for storing raw binary data. 
  5. BooleanField: a true / false field. The default form widget for this field is a CheckboxInput. 
  6. CharField: this is a text field for storing small strings. 
  7. DateTimeField: this is a date, represented in Python by a datetime.date instance. 
  8. DecimalField: this is a fixed precision decimal number, represented in Python by a Decimal instance. 
  9. DurationField: a field for storing time periods. 
  10. EmailField: This is a CharField that verifies that the value is a valid email address. 
  11. FileField: this is a file upload field. 
  12. FloatField: this is a floating point number represented in Python by a floating instance. 
  13. ImageField: it inherits all the attributes and methods of FileField, but also requires that the downloaded object be a valid image. 
  14. IntegerField: this is an entire field. The values ​​from -2147483648 to 2147483647 are safe in all databases supported by Django. 
  15. NullBooleanField: as a BooleanField, but allows NULL as one of the options. 
  16. PositiveIntegerField: like an IntegerField, but must be positive or zero (0). Values ​​from 0 to 2147483647 are safe in all databases supported by Django. 
  17. SmallIntegerField: like an IntegerField, but only allows values ​​under a certain point (depending on the database). 
  18. TextField: a large text field. The default form widget for this field is a text box. 
  19. TimeField: a field representing the time, represented in Python by a datetime.time instance. 

3 - Creation of a Django model 

Before proceeding to the creation of a model, you must first register your app: Add your app to the mysite/settings.py file:

INSTALLED_APPS = [
'myapp.apps.MyappConfig',
########,
########,
]

Here is an example to create a model named Students with the fields: firstName, lastName, email and adress: Add the code below to the myapp/models.py file

from django.db import models
class Students(models.Model):
name = models.CharField(max_length=25)
email = models.EmailField(max_length=40)
phone = models.IntegerField(max_length=40)
section = models.CharField(max_length=25)

These will create an SQLite table containing the fields: firstName, lastName, email and adress, but not before the necessary migrations are executed: To do this, run from the command line:

python manage.py makemigrations myapp

and then :

python manage.py migrate

In order to be able to manage the model that we have just created, it is therefore necessary to load it in the site's admin area. To do this, simply edit the myapp/admin.py file by adding the lines of code:

from django.contrib import admin
from .models import Students
admin.site.register(Students)

Now if you access the site's admin area, you will find the Students section, which gives you the possibility of editing and modification:

5 - Improved display in the admin area 

We can now improve the display of records in the admin area by displaying the list of names, emails, sections ... For this purpose we must import the admin module from django.contrib on the models.py file:

from django.contrib import admin

We then create a class in the models.py file which inherits from the admin.ModelAdmin class which allows to indicate the list of attributes to display and the attributes according to which the filtering of the records is performed:

class StudentsAdmin(admin.ModelAdmin):
list_display = ('name' , 'email' , 'section')
list_filter = ('name',)

Here is the final code of the models.py file:

from django.contrib import admin
from django.db import models
class Students(models.Model):
name = models.CharField(max_length=25)
email = models.EmailField(max_length=40)
phone = models.IntegerField(max_length=40)
section = models.CharField(max_length=25)

class StudentsAdmin(admin.ModelAdmin):
list_display = ('name' , 'email' , 'section')
list_filter = ('name',)

There are now some modifications to the level of the admin.py files in which we must import and save the StudentsAdmin class that we just created:

from django.contrib import admin
from studentsApp.models import Students , StudentsAdmin

admin.site.register(Students , StudentsAdmin)

And finally, if you access the admin area you will see that there is actually an improvement in the display of results showing the list of names, emails, sections ... 

Younes Derfoufi
my-courses.net

Leave a Reply