Django Database model

Source: Internet
Author: User

What is project and what is an app before you learn the model?

1. In Django, project represents the configuration, eg. Database configuration, view templates, List of installed apps, and mappings between URL and view functions

2. An app is a set of Django features, usually containing views and models, in the form of Python packages, the app can be easily ported, reused, a project can contain multiple apps, each app is responsible for different functions

The system has a convention on apps: If you're using a Django data model, you have to create a Django app, and the model must be placed in apps;

1. In Pycharm, the terminal executes the following command to create the app

Python manage.py Startapp Books

2. Write the model code in models.py

 fromDjango.dbImportModels#Create your models here.classPublisher (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) website=models. Urlfield ()def __unicode__(self):returnSelf.nameclassmeta:ordering= ['name']classAuthor (models. Model): First_Name= Models. Charfield (max_length=30) last_name= Models. Charfield (max_length=40) Email=models. Emailfield ()def __unicode__(self):return '%s%s'%(Self.first_name, Self.last_name)classBook (models. Model): Title= Models. Charfield (max_length=100) Author=models. Manytomanyfield (Author) Publisher=models. ForeignKey (Publisher) publication_date=models. Datefield ()def __unicode__(self):returnSelf.title

Note: Class meta represents meta information for the model, eg. default sort; The __unicode__ method can do any processing to return a string representation of an object, similar to ToString () in Java

3. Verifying the legitimacy of the model--grammar and logic are correct

Python manage.py Validate

4. Print out SQL (you can see the relationship between the Django model and the built-in SQL)

Python manage.py sqlall Books

The database does not create any tables at this time

5. Create a table

Python manage.py syncdb

6. Log in to the database command-line client

Python manage.py Dbshell (Django automatically detects the use of different command-line clients based on the database information configured in the configuration file)

#########################################################################

How can I use the API provided by Django for CRUD operations?

1. Create an Object

P1 = Publisher (name="O ' Reilly", address='ten Fawcett St.', city='Cambridge', state_province='MA', country='U.S.A.', website='http://www.oreilly.com/') P1.save ()

P2 = Publisher.objects.create (name="O ' Reilly", address=' FawcettSt.', city=' Cambridge', state_province='MA', country='U.S.A.', website='/http www.oreilly.com/' )

2. Querying objects

Publisher_list = Publisher.objects.all ()#returns Queryset, a pseudo-list objectpublisher_list= Publisher.objects.filter (name='Jerry')#Exact match name herepublisher_list= Publisher.objects.filter (name__contains='Jerry')#Fuzzy match here namepublisher_list= Publisher.objects.filter (name__contains='Jerry'). Order_by ('name')#Sort by the alphabetical listpublisher_list= Publisher.objects.filter (name__contains='Jerry'). Order_by ('-name')#Reverse alphabetical orderPublisher= Publisher.objects.get (name='Jerry')#returns a single object, this method throws an exception, Publisher.doesnotexist, multipleobjectsreturned

3. Updating objects

P1 = Publisher (name="O ' Reilly", address='ten Fawcett St.', city='Cambridge', state_province='MA', country='U.S.A.', website='http://www.oreilly.com/') P1.save () P1.name='Update name'P1.save ()#This method updates all fields, affects performance, and may cause race conditionsPublisher.objects.filter (ID=52). Update (name='Apress Publishing')#This method updates only the specified fields and can have multiple parameters

4. Deleting objects

p = Publisher.objects.get (name="O ' Reilly") P.delete () Publisher.objects.all () Publisher.objects.all (). Delete ()  # use with caution

Django Database model

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.