About me

Saturday, May 12, 2018

Learn Python Django chap 3 (Creating Models)


Friends this is the continuation from its previous post so i kindly recommend you to read that post i have made this very simple even a complete beginner can learn it and if you are beginner in python then you need to learn python i have made that blog also for that click this link

                          

 

                                     MODELS

                 The Models is the single, definitive source of truth about your data contains the essential fields and behaviors of the data you’re storing. The goal is to define your data model in one place and automatically derive things from it. simply models is a class that represents table or collection in our DB, and where every attribute of the class is a field of the table or collection. Models are defined in the app/models.py (in our example: myproj/models.py)
                 
                                    here is an example of a model creation


from django.db import models
class Mytable(models.Model):

   website = models.CharField(max_length = 50)
   mail = models.CharField(max_length = 50)
   name = models.CharField(max_length = 50)
   phonenumber = models.IntegerField()
 class Meta:
      db_table = "dreamreal"
Each model is represented by a class that subclasses django.db.models.Model. Each model has a number of class variables, each of which represents a database field in the model.if you don't know python it will be beneficial to learn it from here. Each field is represented by an instance of a Field class – e.g., CharField for character fields and IntegerField for integers. This tells Django what type of data each field holds.The name of each Field instance (e.g. website or mail) is the field’s name, in machine-friendly format. You’ll use this value in your Python code, and your database will use it as the column name.Some Field classes have required arguments. CharField, for example, requires that you give it a max_length. That’s used not only in the database schema, but in validation, as we’ll soon see.Django supports all the common database relationships: many-to-one, many-to-many, and one-to-one.

ACTIVATING MODELS
The small bit of model code gives Django a lot of information. With it, Django is able to create database and schema (CREATE TABLE statements) for this app.Create a Python database-access API for accessing the Mytable objects.And don't forget that we need to tell our project that the "myapp" app is installed.for that we need to add the To include the app in our project, we need to add a reference to its configuration class in the INSTALLED_APPS setting.  To do so, update INSTALLED_APPS tuple in the settings.py file of your project (add your app name). after that the tuple will look like :


INSTALLED_APPS = (
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
'django.contrib.staticfiles',
 'myproj',
 )
i have already mentioned this before now django knows to inclulde your app in here it is "myproj"
now lets run another ccommand
$ python manage.py makemigrations myproj
You will see something similar to the following:
Migrations for 'myproj':
 myproj/migrations/0001_initial.py:
 - Create model Mytable
by doing this you are telling django that you are changing/creating new one and that you’d like the changes to be stored as a migration
Migrations are how Django stores changes to your models (and thus your database schema) - they’re just files on disk. You can read the migration for your new model if you like; it’s the file myproj/migrations/0001_initial.py. There’s a command that will run the migrations for you and manage your database schema automatically - that’s called migrate, and type the command 
$ python manage.py migrate
for more details on this click here
And exactly you should remember this three steps :
  •  Change your models (in models.py).
  •  Run python manage.py makemigrations to create migrations for those changes
  •  Run python manage.py migrate to apply those changes to the database.
The separate commands  in making and applying migrations in here is because you’ll commit migrations to your version control system and ship them with your app; they not only make your development easier, they’re also useable by other developers and in production.
To know more about manage.py click here

 

 

THANKS FOR VIEWING MY BLOG NEXT POST IS ON "WORKING WITH API" HOPE YOU LIKE THIS BLOG PLEASE COMMENT YOUR SUGGESTIONS AND SHARE THIS BLOG TO YOUR FRIENDS AND DON'T FORGET TO FOLLOW THIS BLOG

2 comments: