Django:向管理员公开模型方法

发布于 2024-09-17 07:19:09 字数 379 浏览 5 评论 0原文

示例模型:

class Contestant(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    email = models.EmailField()
    ...

    def send_registration_email(self):
        ...

我希望能够向管理员公开此方法,以便管理人员可以登录并手动调用它。我正在考虑尝试属性属性,但不确定这是否有效。是否也可以公开这样的方法,该方法接受除 self 之外的参数,可能是来自 select 或其他东西的相关对象?

Example model:

class Contestant(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    email = models.EmailField()
    ...

    def send_registration_email(self):
        ...

I'd like to be able to expose this method to the admin so that managers can login and manually call it. I'm thinking about trying property attributes but not sure if that's gonna work. Also is it possible to expose a method like this that takes arguments other than self, possibly related objects from a select or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一枫情书 2024-09-24 07:19:09

您可以将其注册为管理操作

from django.contrib import admin
from myapp.models import Contestant

def send_mail(modeladmin, request, queryset):
    for obj in queryset:
        obj.send_registration_email()

make_published.short_description = "Resend activation mails for selected users"

class ContestantAdmin(admin.ModelAdmin):
    list_display = [...]
    ordering = [...]
    actions = [send_mail]

admin.site.register(Contestant, ContestantAdmin)

You could register it as an admin action.

from django.contrib import admin
from myapp.models import Contestant

def send_mail(modeladmin, request, queryset):
    for obj in queryset:
        obj.send_registration_email()

make_published.short_description = "Resend activation mails for selected users"

class ContestantAdmin(admin.ModelAdmin):
    list_display = [...]
    ordering = [...]
    actions = [send_mail]

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