django how to create models

models.py
from __future__ import unicode_literals

from django.db import models
# Create your models here.
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
class Author(models.Model):
salutation = models.CharField(max_length=10)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'example',
'books'
my app name is book
                                                    Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.                                                                                                                  
C:\Users\hariz>cd mysite
C:\Users\hariz\mysite>manage.py startapp books

C:\Users\hariz\mysite>manage.py makemigrations
Migrations for 'books':
  books\migrations\0001_initial.py:
    - Create model Author
    - Create model Book
    - Create model Publisher
    - Add field publisher to book

C:\Users\hariz\mysite>manage.py makemigrations --name books
No changes detected

C:\Users\hariz\mysite>python manage.py showmigrations books
books
 [X] 0001_initial

C:\Users\hariz\mysite>python manage.py sqlmigrate books 0001_initial
BEGIN;
--
-- Create model Author
--
CREATE TABLE `books_author` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `
salutation` varchar(10) NOT NULL, `first_name` varchar(30) NOT NULL, `last_name`
 varchar(40) NOT NULL, `email` varchar(254) NOT NULL);
--
-- Create model Book
--
CREATE TABLE `books_book` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `ti
tle` varchar(100) NOT NULL, `publication_date` date NOT NULL);
CREATE TABLE `books_book_authors` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY
KEY, `book_id` integer NOT NULL, `author_id` integer NOT NULL);
--
-- Create model Publisher
--
CREATE TABLE `books_publisher` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY
, `name` varchar(30) NOT NULL, `address` varchar(50) NOT NULL, `city` varchar(60
) NOT NULL, `state_province` varchar(30) NOT NULL, `country` varchar(50) NOT NUL
L, `website` varchar(200) NOT NULL);
--
-- Add field publisher to book
--
ALTER TABLE `books_book` ADD COLUMN `publisher_id` integer NOT NULL;
ALTER TABLE `books_book` ALTER COLUMN `publisher_id` DROP DEFAULT;
ALTER TABLE `books_book_authors` ADD CONSTRAINT `books_book_authors_book_id_ed34
33e7_fk_books_book_id` FOREIGN KEY (`book_id`) REFERENCES `books_book` (`id`);
ALTER TABLE `books_book_authors` ADD CONSTRAINT `books_book_authors_author_id_98
4f1ab8_fk_books_author_id` FOREIGN KEY (`author_id`) REFERENCES `books_author` (
`id`);
ALTER TABLE `books_book_authors` ADD CONSTRAINT `books_book_authors_book_id_8714
badb_uniq` UNIQUE (`book_id`, `author_id`);
ALTER TABLE `books_book` ADD CONSTRAINT `books_book_publisher_id_189e6c56_fk_boo
ks_publisher_id` FOREIGN KEY (`publisher_id`) REFERENCES `books_publisher` (`id`
);
COMMIT;

C:\Users\hariz\mysite>manage.py shell
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
> from books.models import Publisher
>>> newPublisher=Publisher(name='hari',address='trichy',city='trichy',state_prov
ince='tamilnadu',country='india')
>>> newPublisher.save
<bound method Publisher.save of <Publisher: Publisher object>>
>>> newPublisher.name='guru'
>>> newPublisher.save()
>>> new1Publisher=Publisher.objects.get(id=1)
>>> new1Publisher.name
u'Apress'
>>> new1Publisher=Publisher.objects.get(id=3)
>>> new1Publisher.name
u'guru'

Comments

Popular Posts