Record Django development experience _python

Source: Internet
Author: User

Conceptual level

Summarized

Django is a web framework that belongs to MVC.

Model: Responsible for dealing with the database

View: Responsible for acquiring or enhancing data obtained from models

Controller: This is Django itself.

The difference between project and app

Project: Provides a variety of profiles App: A combination of features, including model and view, that requires adding "__init__.py" to the root directory so that Python can recognize

The difference between Manytomany and ForeignKey

ForeignKey Example:

Employee Clock-in Record-employees can have multiple clock-out records, but one card recording can only have a single employee attribute (One-to-many)

Manytomany Example:

An article can have multiple tags, and a label corresponding to the article can also be multiple (many to many)

Manytomany also has a special structure, very similar to Twitter's follow.

Suppose a follow B, but B does not follow a, the follow between them is not equal, to achieve this relationship, to set up in the meta Symetric=false

Sexycode

This is some code that I feel "sexually dead" when I understand it:

Lambda X, Y:x+y 

The lambda in fact and JS in the anonymous function has this similar role, the name of trouble, it is thrown in, lambda default back to the value inside, so do not need to return

Map (Lambda x:x+1, [1,2,3]) #得到 [2,3,4] 

Which is that each one of the preceding functions is executed

Reduce (lambda x, Y:x+y, [1,2,3]) #得到6 

That is, each and every previous element performs one action

[I for I in xrange (0,100) if i%2==0] 

Get all even numbers below 100. Academic is called "list derivation", but in real cases it is a good tool for traversal.

Q (**{key:value}) 

When I was training, I learned that Q, f these two Django functions do not accept strings, that is, Q ("Abc=1") is not acceptable. But the sexy part of the code is to completely get rid of this binding (in fact, rewrite the keyword), you can build your favorite query segments, which is useful in constructing the search function. Like what:

Q_dict = reduce (lambda x,y:x&y, [Q (**{"%s__slug_name"% taxonomy:request.) Get.get (Taxonomy)}
    ) for the taxonomy in request. Get.keys () If taxonomy in Searchable_list])
@property 

The use of the @ symbol is called the "decorator", which is a personal feeling that Python is more beautiful than any other language, how to construct a decorator, or look at a document. This is only to say that the use of @property in class, this function automatically becomes class attributes, which and JS set, get very much like
The use of Super Super class, Super one out, the inherited child class all do not execute its own function, but the execution of super specified functions
Annotate and aggregate. These two guys are very useful when constructing new query_set. For example, to count the total price of a computer, use in Computermanager

def get_query_set (self):
query_set = Super (Computermanager, self). Get_query_set ()
query_set = Query_ Set.annotate (price=sum (' Devices__price '))

In this way, each computer has a total price. This is very convenient when structuring a set of goods, but Admin.py's writing is a bit verbose.

Error set

When fetching data: Xxmanager object is not iterable

This is because Django does not draw SQL statements and queries (so it is impossible to get SQL statements) during code execution. So you need to use. Get (),. All (),. Filter to obtain real data entities

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.