django manager

django manager

In the statement Book.objects.all () in, objects is a special property, it needs by querying the database.

In short, the module manager is an object, Django database query module through it. Django each module has at least a manager, you can create a custom manager to customize the database access.

Create a custom manager: 1) additional manager Method 2 initial QuerySet repair manager returned).

1) add extra Manager methods

Additional manager method is the preferred way to add a table-level functions for the module.

For example, we define a model for the Book title_count () method, which requires a key, returns a number of books that keyword. (This example a little far-fetched, but it can explain how managers work.)

# models.py
from django.db import models

class BookManager(models.Manager):
    def title_count(self, keyword):
        return self.filter(title__icontains=keyword).count()
 
class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()
    num_pages = models.IntegerField(blank=True, null=True)
    objects = BookManager()   # 语法就是object=SomeManager
 
  def __unicode__(self):
    return self.title

With this manager, we can now do this:

>>> Book.objects.title_count('django')
4
>>> Book.objects.title_count('python')
18

Why do we want to add a title_count () method do? Query is frequently used to encapsulate, so we do not repeat encoded.
Modify the initial Manager QuerySets

substantially QuerySet manager returns all objects in the system. For example, Book.objects.all()return all the books in the book database.

We can cover Manager.get_query_set () method to override the base QuerySet manager. get_query_set () returns a QuerySet accordance with your request.

This example also pointed out another interesting technique: using multiple manager in the same model. If you like, you can add multiple manager () example for your model. This is a simple way to add a generic filter for the model.

E.g:

class MaleManager(models.Manager):
  def get_query_set(self):
    return super(MaleManager, self).get_query_set().filter(sex='M')
 
class FemaleManager(models.Manager):
  def get_query_set(self):
    return super(FemaleManager, self).get_query_set().filter(sex='F')
 
class Person(models.Model):
  first_name = models.CharField(max_length=50)
  last_name = models.CharField(max_length=50)
  sex = models.CharField(max_length=1, choices=(('M', 'Male'), ('F', 'Female')))
  people = models.Manager()
  men = MaleManager()
  women = FemaleManager()

This example allows you to perform Person.men.all(), Person.women.all(), Person.people.all()query, generate the results you want.

If you use custom Manager objects, please note, Django first encountered Manager (in a position which is defined in the model prevail) have a special status. Django will be the first Manager defined as the default Manager, many parts of Django (but not including the admin application) will definitely use this as a model manager. The conclusion is that you should be careful to choose your default manager. Because coverage get_query_set (), you may receive a return of as useless, you have to avoid this.

Guess you like

Origin www.cnblogs.com/wangjiale1024/p/11391236.html