Django ModelChoiceField没有加号按钮 [英] Django ModelChoiceField has no plus button
问题描述
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)
我可以在示例代码中出错,所以看到这些帖子和示例:
- RelatedFieldWidgetWrapper
- 更多相关eldWidgetWrapper - 我自己的弹出窗口
- Django管理员 - 如何在自定义管理表单中为多对多字段添加绿色加号
- 如何在自定义窗口小部件周围手动使用RelatedFieldWidgetWrapper?
- Django:override RelatedFieldWidgetWrapper
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:
- RelatedFieldWidgetWrapper
- More RelatedFieldWidgetWrapper – My Very Own Popup
- Django admin - How can I add the green plus sign for Many-to-many Field in custom admin form
- How can I manually use RelatedFieldWidgetWrapper around a custom widget?
- Django: override RelatedFieldWidgetWrapper
这篇关于Django ModelChoiceField没有加号按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!