Social Icons

Monday, June 29, 2020

Django Models

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

No comments:

Post a Comment

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (1) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (167) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (32) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (138) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates