django-admin 调用模型时调用函数
我的模型中有这样的内容:
class Artykul(models.Model):
id = models.AutoField(max_length = 5, primary_key = True)
tytul = models.CharField(max_length = 255)
kategoria = models.ManyToManyField(Title, limit_choices_to = choices_limit)
dodano = models.DateField(auto_now_add = True)
autor = models.CharField(max_length = 255)
glowny_obrazek = models.ImageField(blank = True, upload_to="uploads/artykul_main_img")
tresc = PlaceholderField('tresc')
def show_categories(self):
return ', '.join( self.kategoria.values_list('title', flat=True) )
def prepare_choices_limit(self):
return {'page__level__exact': 1}
def __unicode__(self):
return self.tytul
如您所见,我的 ManyToManyField 中有 var Choices_limit 。我想让它变得动态,以允许某些用户根据他们的权限拥有不同的选项。我可以通过函数prepare_choices_limit()来做到这一点。问题是:这整个想法是否好,如果不好,如何以不同的方式实现这种效果?或者,如果它很好,我如何在每次用户登录管理面板时调用此函数。我不希望每次调用 Model 时都调用它,因为我不需要对站点的普通用户进行权限检查,只需对管理员进行权限检查。
提前致谢 米哈乌
I have something like this in my model:
class Artykul(models.Model):
id = models.AutoField(max_length = 5, primary_key = True)
tytul = models.CharField(max_length = 255)
kategoria = models.ManyToManyField(Title, limit_choices_to = choices_limit)
dodano = models.DateField(auto_now_add = True)
autor = models.CharField(max_length = 255)
glowny_obrazek = models.ImageField(blank = True, upload_to="uploads/artykul_main_img")
tresc = PlaceholderField('tresc')
def show_categories(self):
return ', '.join( self.kategoria.values_list('title', flat=True) )
def prepare_choices_limit(self):
return {'page__level__exact': 1}
def __unicode__(self):
return self.tytul
As you can see I have var choices_limit in my ManyToManyField. I'd like to make it dynamic to allow certain users to have diffirent options based on their permissions. I can do it by functions prepare_choices_limit(). Question is: if this whole idea is good and if its not how to achieve this effect in diffirent way? Or if its good how can I call this function each time user is logging to admin panel. I dont want it to be called each time Model is called, because I dont need to make permission check for normal users of site, just for administrators.
Thanks in advance
Michał
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)