如何让用户从change_view和add_view进入验证

发布于 2024-10-12 14:59:05 字数 853 浏览 2 评论 0原文

我尝试使用 request.user 对象的验证来限制 django 管理站点中特定用户的某些行的更新。我的印象是我需要重写 ModelAdmin change_view 方法以将请求对象传递到表单。我已经查看了 django.contrib.admin.options 中的change_view方法,但是作为一个刚接触django的人,我无法理解change_view方法中我需要进行这些修改的位置。任何指向正确方向的指针都会很棒。

class IssuesAdmin(admin.ModelAdmin):
    def change_view(self, request, object_id, extra_context=None):
        #modify lines to pass request to form

    form = IssuesAdminForm


class IssuesAdminForm(forms.ModelForm):
    class Meta:
        model = Issues

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(IssuesAdminForm, self).__init__(*args, **kwargs)


    def clean_product(self):
        if self.request.user.name=='someone'
            return self.cleaned_data["product"]
        else:
            raise forms.ValidationError("Nope!")

I'm trying to use validation with the request.user object to restrict updates to some rows for specific users within the django admin site. I get the impression I need to override the ModelAdmin change_view method to pass the request object to the form. I've looked at the change_view method in django.contrib.admin.options, but as someone very new to django, am having trouble understanding where in the change_view method I need to make these modifications. Any pointers in the right direction would be great.

class IssuesAdmin(admin.ModelAdmin):
    def change_view(self, request, object_id, extra_context=None):
        #modify lines to pass request to form

    form = IssuesAdminForm


class IssuesAdminForm(forms.ModelForm):
    class Meta:
        model = Issues

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(IssuesAdminForm, self).__init__(*args, **kwargs)


    def clean_product(self):
        if self.request.user.name=='someone'
            return self.cleaned_data["product"]
        else:
            raise forms.ValidationError("Nope!")

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

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

发布评论

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

评论(1

双马尾 2024-10-19 14:59:05
class IssuesAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, extra_context=None): #remember to edit also add_view()... etc
    self.form.request = request

form = IssuesAdminForm

class IssuesAdminForm(forms.ModelForm):
    class Meta:
       model = Issues

    def __init__(self, *args, **kwargs):

       self.request = # do what you need ;)
       super(IssuesAdminForm, self).__init__(*args, **kwargs)


def clean_product(self):
    if self.request.user.name=='someone'
        return self.cleaned_data["product"]
    else:
        raise forms.ValidationError("Nope!")
class IssuesAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, extra_context=None): #remember to edit also add_view()... etc
    self.form.request = request

form = IssuesAdminForm

class IssuesAdminForm(forms.ModelForm):
    class Meta:
       model = Issues

    def __init__(self, *args, **kwargs):

       self.request = # do what you need ;)
       super(IssuesAdminForm, self).__init__(*args, **kwargs)


def clean_product(self):
    if self.request.user.name=='someone'
        return self.cleaned_data["product"]
    else:
        raise forms.ValidationError("Nope!")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文