强制用户在添加模型时添加一些InlineModelAdmin的实例 [英] Force the user to add an instance of some InlineModelAdmin when adding a model

查看:90
本文介绍了强制用户在添加模型时添加一些InlineModelAdmin的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserAdmin ,我定义了一个 UserProfileInline ,如下所示:

I have a UserAdmin, and I defined a UserProfileInline like this:

from ...  
from django.contrib.auth.admin import UserAdmin as UserAdmin_

class UserProfileInLine(admin.StackedInline):
    model = UserProfile
    max_num = 1
    can_delete = False
    verbose_name = 'Profile'
    verbose_name_plural = 'Profile'

class UserAdmin(UserAdmin_):
    inlines = [UserProfileInLine]

我的 UserProfile 模型有一些必填字段。

My UserProfile model has some required fields.

我想要的是强制用户不仅输入用户名&重复密码,还要至少输入必需的字段,以便创建 UserProfile 实例并将其关联到用户

What I want is to force the user not only to enter the username & repeat password, but also to enter at least the required fields so that the UserProfile instance is created and associated to the User that is being added.

如果在创建用户时在 UserProfileInline 的任何字段中输入任何内容,则会验证形式没有问题,但如果我不触摸任何字段,它只是创建用户,没有任何事情发生在 UserProfile

If I enter anything in any field of UserProfileInline when creating the user, it validates the form without problem, but if I don't touch any field, it just creates the User and nothing happens with the UserProfile.

任何想法?

推荐答案

检查最近的答案在Django中扩展用户配置文件。管理员创建用户,您需要将内联表单 empty_permitted 属性设置为 False 。就像

Check recent answer Extending the user profile in Django. Admin creation of users , you need to set the empty_permitted attribute of the form of the inline , to be False. Just like

class UserProfileForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(UserProfileForm, self).__init__(*args, **kwargs)
        if self.instance.pk is None:
            self.empty_permitted = False # Here

    class Meta:
        model = UserProfile


class UserProfileInline(admin.StackedInline):                                     
    form = UserProfileForm  

另一个可能的解决方案可能是创建自己的 Formset (继承自 BaseInlineFormSet ),如此链接

Another possible solution could be to create your own Formset (that inherits from BaseInlineFormSet), like suggested in this link.

可能是这样的:

class UserProfileFormset(BaseInlineFormSet):
    def clean(self):
        for error in self.errors:
            if error:
                return
        completed = 0
        for cleaned_data in self.cleaned_data:
            # form has data and we aren't deleting it.
            if cleaned_data and not cleaned_data.get('DELETE', False):
                completed += 1

        if completed < 1:
            raise forms.ValidationError('You must create a User Profile.')

然后在 InlineModelAdmin 中指定该表单:

class UserProfileInline(admin.StackedInline):
    formset = UserProfileFormset
    ....

关于这个第二个选择是,如果UserProfile模型不需要填写任何字段,它仍然会要求您在至少一个字段中输入任何数据。第一种模式不是。

The good thing about this second option is that if the UserProfile model doesn't require any field to be filled, it will still ask for you to enter any data in at least one field. The first mode doesn't.

这篇关于强制用户在添加模型时添加一些InlineModelAdmin的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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