django--关于Managers
来源:程序员人生 发布时间:2015-01-24 09:08:21 阅读次数:2838次
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", "能下载文件"),
)
然后再仔细看了1下文档,发现了文章开头的那段话,大概意思就是定义的第1个manager将会作为默许manager....我果断加了1个ALL,现在可以正确显示了,代码以下:
class AllFileManager(models.Manager):
def get_queryset(self):
return super(AllFileManager, self).get_queryset().all()
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:
allFiles = AllFileManager() # <-------------------------------------------------------------
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.
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠