Django ModelChoiceField没有加号按钮 [英] Django ModelChoiceField has no plus button

查看:293
本文介绍了Django ModelChoiceField没有加号按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义用户制作一个Django应用程序。我已经概述了我的问题的关键组成部分,缺少的代码用...表示。我的自定义用户模型具有外键关系如下:

  class MyCustomUser(models.AbstractBaseUser,models.PermissionsMixin)
...
location = models.ForeignKey(位置)

类位置(models.Model)
name = models.CharField(max_length = 50,blank = True,null = True)

我已经编写了一个包含此字段的自定义用户表单,如下所示:

  class MyCustomUserCreationForm(models.ModelForm)
...
location = forms.ModelChoiceField(Location.objects.all ())

这一切似乎都正常工作,但是右边没有加号按钮的选择字段的位置。我想要在创建用户时添加一个位置,就像在 Django教程。根据此问题,我可能不会如果我没有更改模型的权限,请参阅绿色加号,但是我以超级用户身份登录了所有权限。任何想法我在做什么错?

解决方案

你需要设置一个 RelatedFieldWidgetWrapper wrapper in your model form:


RelatedFieldWidgetWrapper(在django.contrib.admin.widgets中找到)
被使用在管理页面中,包含外键
控件上的功能以添加新的相关记录。 (英文:把小绿色的加号放在控件的右边)




  class MyCustomUserCreationForm(models.ModelForm)
...
location = forms.ModelChoiceField(queryset = Location.objects.all())

def __init __(self,* args, ** kwargs)
super(MyCustomUserCreationForm,self).__ init __(* args,** kwargs)
rel = ManyToOneRel(self.instance.location.model,'id')
self .fields ['location']。widget = RelatedFieldWidgetWrapper(self.fields ['location']。widget,rel,self.admin_site)

我可以在示例代码中出错,所以看到这些帖子和示例:




I'm making a Django app with custom users. I've outlined the key components of my problem below, missing code is denoted by '...'. My custom user model has a foreign key relationship as follows:

class MyCustomUser(models.AbstractBaseUser, models.PermissionsMixin)
    ...
    location = models.ForeignKey(Location)

class Location(models.Model)
    name = models.CharField(max_length=50, blank=True, null=True)

I've written a custom user form that includes this field as follows:

class MyCustomUserCreationForm(models.ModelForm)
    ...
    location = forms.ModelChoiceField(Location.objects.all())

This all appears to be working correctly, however, there is no plus button to the right of the select field for location. I want to be able to add a location when I create a user, in the same way that you can add polls when creating choices in the Django tutorial. According to this question, I might not see the green plus if I don't have permission to change the model, but I am logged in as a superuser with all permissions. Any idea what I'm doing wrong?

解决方案

You need to set a RelatedFieldWidgetWrapper wrapper in your model form:

The RelatedFieldWidgetWrapper (found in django.contrib.admin.widgets) is used in the Admin pages to include the capability on a Foreign Key control to add a new related record. (In English: puts the little green plus sign to the right of the control.)

class MyCustomUserCreationForm(models.ModelForm)
    ...
    location = forms.ModelChoiceField(queryset=Location.objects.all())

    def __init__(self, *args, **kwargs):
        super(MyCustomUserCreationForm, self).__init__(*args, **kwargs)
        rel = ManyToOneRel(self.instance.location.model, 'id') 
        self.fields['location'].widget = RelatedFieldWidgetWrapper(self.fields['location'].widget, rel, self.admin_site)

I could make a mistake in the example code, so see these posts and examples:

这篇关于Django ModelChoiceField没有加号按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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