码迷,mamicode.com
首页 > 其他好文 > 详细

django--关于Managers

时间:2015-01-21 18:19:36      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

Default managers

If you use custom 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 with.


开始写了两个判断是否私密的manager,然后发现admin里面的显示变成了只能显示私密的File:

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", "能编辑文件"),
            ("can_list_file", "能看文件列表"),
            ("can_delete_file", "能删除文件"),
            ("can_download_file", "能下载文件"),
        )

然后再仔细看了一下文档,发现了文章开头的那段话,大概意思就是定义的第一个manager将会作为默认manager....我果断加了一个ALL,现在可以正确显示了,代码如下:

<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", "能编辑文件"),
            ("can_list_file", "能看文件列表"),
            ("can_delete_file", "能删除文件"),
            ("can_download_file", "能下载文件"),
        )

That‘s all.

django--关于Managers

标签:

原文地址:http://blog.csdn.net/tmpbook/article/details/42970645

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!