Django管理员 - 与主要模型一起验证内联 [英] Django Admin - Validating inlines together with main models

查看:364
本文介绍了Django管理员 - 与主要模型一起验证内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常复杂的验证要求,我不能让Django管理员满足它。



我有一个主要的模型( django。 contrib.auth.models.User )和几个看起来像

 的类型SomeProfile(models。模型):
user = models.OneToOneField(User)
#more fields

我想检查一下,如果用户属于某个组,那么它有相应的配置文件。所以如果用户在组 Foo 中,他应该有一个非空的 FooProfile



我将哪个验证规则放在哪里?我不能把它放在模型里。实际上,当表单被验证时,用户尚未创建,因此我无法访问他的组。所以我需要诉诸表单验证。这是我所说的:

  class UserAdminForm(forms.ModelForm):

A自定义表单添加不能在
模型中生成的验证规则我们检查属于各个组的用户实际上是
具有相应的配置文件

class Meta:
model =用户

def clean(self):
#这里是我要放置验证的地方

class FooInline(admin.TabularInline) :
model = FooProfile
max_num = 1


class UserAdmin(admin.ModelAdmin):
model = User
form = UserAdminForm
inlines = [FooInline]

admin.site.register(User,UserAdmin)

我的问题是在 UserAdminForm.clean()之内我无法访问内联中发布的数据。所以我可以通过检查 self.cleaned_data ['groups'] 来判断用户是否在Foo组中,但是我无法判断一个 FooProfile 被传送。


如何检查此验证要求?


编辑:



我尝试更好地解释问题,因为已经有一个答案中的误解。



当我创建一个新用户时,我有一个问题。事实是,配置文件是强制性的(根据组)。说管理员创建一个新用户;那么我必须在各种GroupProfiles的管理表单中添加内联。



如何检查正确的配置文件是否不为空?我不能使用 User 模型的 clean()方法,因为在那里我无法检查用户所属的组至:它尚未创建。



我只能访问 clean()表单的方法 - 但是我没有关于配置文件的信息,因为这些信息是内嵌的。

解决方案

1



我一直在寻找,这些东西如何工作,我发现一个非常相似的问题 here



2



有一种方法可以同时获取所有数据,也可以通过这种方式找到问题的答案



  class UserAdminForm(forms.ModelForm):

添加验证规则的自定义窗体es不能生活在
模型中。我们检查属于各个组的用户实际上是
具有相应的配置文件。

class Meta:
model =用户

def clean(self):
self.data#< - 这里是所有请求的数据
self.data ['groups']
self.data ['profile_set-0-comments']#某些字段
#某些验证

return self.cleaned_data


I have quite a complex validation requirement, and I cannot get Django admin to satisfy it.

I have a main model (django.contrib.auth.models.User) and several models which look like

class SomeProfile(models.Model):
    user = models.OneToOneField(User)
    # more fields

I want to check that, if the user belongs to some group, then it has the corresponding profile. So if user is in group Foo he should have a non empty FooProfile.

Where do I put this validation rule? I cannot put it in the model. Indeed, the user is not created yet when the form is validated, hence I cannot access his groups. So I need to resort to form validation. This is what I put:

class UserAdminForm(forms.ModelForm):
    """
    A custom form to add validation rules which cannot live in the
    model. We check that users belonging to various groups actually
    have the corresponding profiles.
    """
    class Meta:
        model = User

    def clean(self):
        # Here is where I would like to put the validation

class FooInline(admin.TabularInline):
    model = FooProfile
    max_num = 1


class UserAdmin(admin.ModelAdmin):
    model = User
    form = UserAdminForm
    inlines = [FooInline]

admin.site.register(User, UserAdmin)

My problem is that inside UserAdminForm.clean() I do not have access to the data posted inside the inlines. So I can tell whether the user is in group Foo by inspecting self.cleaned_data['groups'], but I have no way to tell whether a FooProfile was transmitted.

How do I check this validation requirement?

Edit:

I try to explain the issue better, because there has been a misunderstading in an answer.

I have an issue when I create a new user. The fact is that the profiles are mandatory (according to the groups). Say an admin creates a new user; then I have to add inlines in the admin form for the various GroupProfiles.

How do I check that the right profiles are not null? I cannot use the clean() method of the User model, because in there I cannot check what groups the user belongs to: it has not been created yet.

I can only access the information about the groups in the clean() method of the form - but there I do not have the information about the profiles, since this information is submitted trhough inlines.

解决方案

1

well i have been looking around, how all this stuff works, and i found one question very similar here.

2

There are one way to get all the data at the same time maybe with this you can find the answer to your problem

class UserAdminForm(forms.ModelForm):
    """
    A custom form to add validation rules which cannot live in the
    model. We check that users belonging to various groups actually
    have the corresponding profiles.
    """
    class Meta:
        model = User

    def clean(self):
        self.data # <--here is all the data of the request
        self.data['groups']
        self.data['profile_set-0-comments'] # some field
        # some validations

        return self.cleaned_data

这篇关于Django管理员 - 与主要模型一起验证内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆