python - Django ModelChoiceField has no plus button -


i'm making django app custom users. i've outlined key components of problem below, missing code denoted '...'. custom user model has foreign key relationship 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 custom user form includes field follows:

class mycustomusercreationform(models.modelform)     ...     location = forms.modelchoicefield(location.objects.all()) 

this appears working correctly, however, there no plus button right of select field location. want able add location when create user, in same way can add polls when creating choices in django tutorial. according to question, might not see green plus if don't have permission change model, logged in superuser permissions. idea i'm doing wrong?

you need set relatedfieldwidgetwrapper wrapper in model form:

the relatedfieldwidgetwrapper (found in django.contrib.admin.widgets) used in admin pages include capability on foreign key control add new related record. (in english: puts little green plus sign right of 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 make mistake in example code, see these posts , examples:


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -