Saturday 2 August 2014

how create a model in django

Standard

model is a very important part of any framework now i will describe how create a model in django and how it work. model use for data source a model is connector between controller and database, in django connector between view and database actually django is MTV so view call a function from model module and this function is a table name . this function load respective row from database and give  row array

django create table automatic if you add your table in model then when synchronized then this all table will create in mysql database

ok now start by creating the data model for a user table, Open store/models.py in your editor and type the following code:



from django.db import models
class User(models.Model):
 name = models.TextField(max_length=200)

 email=models.EmailField(max_length=254)
 password = models.TextField(max_length=200)

 for use this model, we first need to activate it in our Django project. editing settings.py, looking for the INSTALLED_APPS variable, and adding application name (store.app) to it:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django_bookmarks.bookmarks',

)

now type this command in your terminal

$ python manage.py syncdb

now type this command for open your django shell

$ python manage.py shell

type this command for test database table fecth

from app.models import *
user1 = user(name='Netai Nayek','net.nayek@gmail.com','netainayek123')
user1.save()

here a row store in user table

for better reference follow

https://docs.djangoproject.com/en/dev/ref/models/fields/