Django -- about Managers, django -- Managers

Source: Internet
Author: User

Django -- about Managers, django -- Managers
Default managers

If you use objecm Manager objects, take note that the first Manager Django encounters (in the order in which they're defined in the model) has a special status. django interprets the first Manager defined in a class as the "default" Manager, and several parts of Django (including dumpdata) will use that Manager exclusively for that model. as a result, it's a good idea to be careful in your choice of default manager in order to avoid a situation where overriding get_queryset () results in an inability to retrieve objects you 'd like to work.


I started to write two managers to determine whether the manager is private or not, and found that the display in admin becomes a File that can only be private:

Class PrivateFileManager (models. manager): def get_queryset (self): return super (PrivateFileManager, self ). get_queryset (). filter (public = True) class PublicFileManager (models. manager): def get_queryset (self): return super (PublicFileManager, self ). get_queryset (). fileter (public = False) class File (models. model): file = models. fileField (upload_to = 'medias') public = models. booleanField (default = True) # has default project = models. foreignKey (Project, related_name = "projects") tag = models. manyToManyField (Tag, related_name = "tags") uploader = models. foreignKey (User, related_name = "users") group = models. manyToManyField (Group, related_name = "groups") temp_delect = models. booleanField (default = False) # has default desc = models. textField (blank = True, null = True) upload_date = models. dateTimeField (auto_now_add = True) change_date = models. dateTimeField (auto_now = True) # Managers blow: privateFiles = PrivateFileManager () publicFiles = PublicFileManager () def _ unicode _ (self): return self. file. name class Meta: permissions = ("can_edit_file", "file editable"), ("can_list_file", "file list visible"), ("can_delete_file ", "can delete files"), ("can_download_file", "can download files "),)

Then I carefully read the document and found the beginning of the article, which means that the first manager defined will be used as the default manager .... I decisively added an ALL, which can be correctly displayed now. The Code is as follows:

<Span style = "color: # ff0000;"> class AllFileManager (models. manager): def get_queryset (self): return super (AllFileManager, self ). get_queryset (). all () </span> class PrivateFileManager (models. manager): def get_queryset (self): return super (PrivateFileManager, self ). get_queryset (). filter (public = True) class PublicFileManager (models. manager): def get_queryset (self): return super (PublicFileManager, self ). get_queryset (). fileter (public = False) class File (models. model): file = models. fileField (upload_to = 'medias') public = models. booleanField (default = True) # has default project = models. foreignKey (Project, related_name = "projects") tag = models. manyToManyField (Tag, related_name = "tags") uploader = models. foreignKey (User, related_name = "users") group = models. manyToManyField (Group, related_name = "groups") temp_delect = models. booleanField (default = False) # has default desc = models. textField (blank = True, null = True) upload_date = models. dateTimeField (auto_now_add = True) change_date = models. dateTimeField (auto_now = True) # Managers blow: <span style = "color: # ff0000;"> allFiles = AllFileManager () </span> privateFiles = PrivateFileManager () publicFiles = PublicFileManager () def _ unicode _ (self): return self. file. name class Meta: permissions = ("can_edit_file", "file editable"), ("can_list_file", "file list visible"), ("can_delete_file ", "can delete files"), ("can_download_file", "can download files "),)

That's all.

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.