Django Learning note Five Django database Manytomanyfield and ForeignKey

Source: Internet
Author: User

1, the model inside the code:
 from__future__ImportUnicode_literals
ImportDjango.utils.timezone asTimeZone
fromDjango.dbImportModels

# Create your models here.
classPublisher (models. Model):
Name = models. Charfield (Max_length=30)
Address = models. Charfield (Max_length=30)
City = models. Charfield (Max_length=30)

def __unicode__( Self):
return Self. Name

classAuthor (models. Model):
Name = models. Charfield (Max_length=30)
email = models. Emailfield ()

def __unicode__( Self):
return Self. Name

classBook (Models. Model):
title = models. Charfield (Max_length=100)
Author = models. Manytomanyfield (Author)
Publisher = models. ForeignKey (Publisher)
Publication = models. Datefield (default=timezone.now ())

def __unicode__( Self):
return Self. title
2. Write data to Database (SQLite)
#coding: Utf-8
fromDjango_book.wsgiImport*
fromBook.modelsImportAuthor,book,publisher
ImportDatetime
ImportRandom

#初始化作者数据
defInitauthor ():
NameList = ['Mike‘,‘Marry‘,‘Jack‘,‘Tom‘]
Emaillist =['[email protected]‘,‘[email protected]‘,‘[email protected]‘,‘[email protected]‘]
Autherlist = []
forIIn range(Len(NameList)):
name = Namelist[i];
email = emaillist[i]
Auther = Author (name=name,Email=email)
Autherlist.append (Auther)
Author.objects.bulk_create (Autherlist)

#初始化出版社数据
defInitpublisher ():
NameList = ['Shanghaipublisher‘, ‘Beijingpublisher‘, ‘Beishidapublisher‘, ‘Qinghuapublisher‘]
CityList = ['Shanghai‘, ‘Beijing‘, ‘Beijing‘, ‘Beijing‘]
AddressList = ['shanghai_address‘, ‘beijing_address‘, ‘beijing_address‘, ‘beijing_address‘]
Publisherlist = []
forIIn range(Len(NameList)):
name = Namelist[i];
City = Citylist[i]
Address = Addresslist[i]
Publisher = Publisher (name=name, City=city,Address=address)
Publisherlist.append (publisher)
Publisher.objects.bulk_create (Publisherlist)

#初始化书数据
defInitbook ():
Booklist = ['python‘,‘HTML‘,‘Django‘,‘Java‘]
Publisherlist = List (Publisher.objects.all ())
Authorlist = List (Author.objects.all ())
forIIn range(Len(Booklist)):
title = Booklist[i];
Publisher = Publisherlist[i]
Book, Created =book.objects.get_or_create (title=title,Publisher=publisher,
Publication=datetime.datetime.now ())
Book.author.add (Authorlist[i])

defDeletebook ():
Bobj = Book.objects.all ()
Bobj.delete ()


defMain ():
# Deletebook ();
# Initauthor ()
# Initpublisher ()
Initbook ()

if__name__ = = '__main__‘:
Main ()

Django Learning note Five Django database Manytomanyfield and ForeignKey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.