Record Django development experiences and django development experiences
Concept
Summary
Django is a Web framework of MVC.
Model: responsible for dealing with databases
View: obtains or enhances data obtained from Models.
Controller: This is Django.
Differences between projects and apps
Project: provides a variety of configuration file apps: the combination of functions, including Model and view, you need to add "_ init _. py" under the root directory, so that Python can recognize
Differences between manytokey and ForeignKey
ForeignKey example:
Employees logging on to work-employees can logging on multiple times, but one swipe record can only have a single employee attribute (one-to-many)
ManyToMany example:
One article can have multiple tags, and one tag can correspond to multiple articles (many to many)
Manytolow also has a special structure, which is very similar to Twitter's Follow.
Assume that A Follow has B, but B does not have Follow A. The Follow between them is not equal. To implement this relationship, set symetric = False in Meta.
SexyCode
This is some code that I think is "sexy to death" after I understand it:
lambda x, y: x+y
Lambda actually works in the same way as the anonymous function in JS. If it is difficult to name a name, it will be lost. By default, Lambda returns the value in it, so no return is required.
Map (lambda x: x + 1, [, 3]) # obtain [2, 3, 4].
That is, each of them executes the previous function once.
Reduce (lambda x, y: x + y, [1, 2, 3]) # obtain 6
That is, each operation is performed with the previous element.
[i for i in xrange(0,100) if i%2==0]
Obtain all the even numbers below 100. Academic name is "list Derivation", but it is a good tool for traversing in real cases.
Q(**{key:value})
During the training, I learned that Q and F do not accept strings, that is, Q ("abc = 1") is unacceptable. However, the sexiness of this Code is to completely get rid of this constraint (in fact, it is to rewrite the keyword pair). You can construct the desired query segment at will, which is very useful in constructing the search function. For example:
q_dict = reduce(lambda x,y: x&y, [ Q(**{"%s__slug_name" % taxonomy:request.GET.get(taxonomy)} ) for taxonomy in request.GET.keys() if taxonomy in SEARCHABLE_LIST ])@property
This @ symbol is used as a modifier. I personally think this is a place where python is more elegant than other languages. It is better to read the document to construct the modifier. If @ property is used in the class, this function will automatically become the class attribute, which is similar to set and get in js.
When super is used, the inherited sub-class does not execute its own function, but executes the function specified by super.
Annotate and aggregate. These two guys are very useful when constructing a new query_set. For example, you can use ComputerManager to calculate the total price of a computer.
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 constructing the product set, but the admin. py compilation is a bit cool.
Error set
When data is retrieved: XXManager object is not iterable
This is because Django does not obtain and query SQL statements during Code Execution (so it is impossible to obtain SQL statements ). So we need to use. get (),. all (),. filter to obtain the real data entity.
Use python + django to write a simple message board (you can leave a message and reply to it) which man has complete code (including the page database)
Python is very popular. I just heard that this is a good thing.
Django learning materials
Django has a tutorial on the website. Learn with patience. I learned it in one day.
Django is not easy to learn. But like python, you can read its source code frequently.
Django documentation is quite complete. It is even better than python. If you are not used to English, you can also find Chinese documents.