django-admin 调用模型时调用函数

发布于 2024-09-25 02:06:10 字数 943 浏览 5 评论 0原文

我的模型中有这样的内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2024-10-02 02:06:10
class Artykuladmin(models.ModelAdmin):
    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        formfield= super(ArtykulAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    if request.user.is_superuser:
        return formfield

    if db_field.name == "kategoria":
        #specify your criteria here:
        qs = Title.objects.filter(page__level__exact=1)
        kwargs["queryset"] = qs

    return db_field.formfield(**kwargs)
class Artykuladmin(models.ModelAdmin):
    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        formfield= super(ArtykulAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    if request.user.is_superuser:
        return formfield

    if db_field.name == "kategoria":
        #specify your criteria here:
        qs = Title.objects.filter(page__level__exact=1)
        kwargs["queryset"] = qs

    return db_field.formfield(**kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文