用Django模型完成更多的任務

jieforest發表於2012-06-07
[i=s] 本帖最後由 jieforest 於 2012-6-7 04:50 編輯

So you have a Django app, but sometimes you find the Django models too constraining. We will guide you through using Django models to get more out of them. This is an intermediate tutorial, as some familiarity with Django is assumed. For example, we assume you know how to write a basic Django model, you know how to override Python methods, as well as how .filter and.exclude work.


We will talk about these topics


1. Proxy Models

2. Overriding .save

3. Using signals

4. Optimizing your DB access using .extra

5. Advanced lookups using Q objects

6. Aggregation and Annotation

7. Using F() expressions


Lets look at some common operations you may want to perform. using Django and how the above Django functionality will help you achieve them.


How can I get two Python representation of the same Database table?


You may want to have two model classes corresponding to a single database table. For example, admin.site.register allows a Model to be registered only once. However, you may want the same model twice in the Admin area. Proxy models can help you do that!

CODE:

from django.contrib.auth.models import User  
class NewUser(User):  
    class Meta.:  
        proxy = True  

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-732101/,如需轉載,請註明出處,否則將追究法律責任。

相關文章