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.
时间: 2024-10-11 11:51:34