Interaction between python django and databases

Source: Internet
Author: User

Download the mysqdb http://www.codegood.com/archives/4 without any issues

1. Create a new app. Python manage. py startapp books2 how to activate the app: edit the settings. py file and find the INSTALLED_APPS settings. INSTALLED_APPS tells Django projects which apps are activated. INSTALLED_APPS = (# 'django. contrib. auth ', # 'django. contrib. contenttypes ', # 'django. contrib. sessions ', # 'django. contrib. sites ', 'mysite. books ',) 3 python manage. run the py validate command to check whether the syntax and logic of your model are correct. If everything is normal, you will see the 0 errors found message. If there is a problem, it will provide a very useful error message to help you correct your model. 4. Generate the create table statement python manage. py sqlall books5 sqlall command. Instead of creating a data TABLE in the database, print the SQL statement segment. Django provides a simpler way to execute these SQL statements. Run the syncdb command: python manage. to create an object for py syncdb6 a, you only need to import the corresponding model class and input each field value to instantiate it. B calls the save () method of the object and saves the object to the database. Django will execute an INSERT Statement c in the background and use Publisher. objects to get objects from the database. Call Publisher. objects. all () to obtain all the Publisher objects in the database. In this case, Django executes a select SQL statement d in the background to add a method _ str _ () to the Publisher object. The _ str _ () method tells Python how to treat an object as a string and use e to select an object: Publisher. objects. all () f Data Filtering Publisher. objects. filter (name = "Apress Publishing") g SQL link usage: Publisher. objects. filter (name _ contains = "press") h icontains (case-insensitive LIKE), startswith and endswith, and range (SQL between query) I get a single object Publisher. objects. get (name = "Apress Publishing") j. Data Sorting Publisher. objects. order_by ("name") k using order_by () seems a bit cool. Most of the time, you usually only sort certain fields to specify the default sorting mode of the model. ordering = ["name"] tells Django that if order_by () is not provided (), class Publisher (models. model): pass class Meta: ordering = ["name"] m limits the returned data Publisher. objects. all () [0] n Delete object p = Publisher. objects. get (name = "Addison-Wesley") p. delete () // 5 TypeError: _ init _ () got an unexpected keyword argument 'maxlength' this error occurs in Django. It seems that maxlength should be used in previous versions. But the new version uses max_length6Error: One or more models did not validate: books. author: "headshot": To use ImageFields, you need to install the Python Imaging Library. get it at http://www.pythonware.com/products/pil. --> download and install it on this website.

 

 

Related Article

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.