Assigning a group while adding user in django-admin -


In my application, I have models that are in the form of a foreign key in the form of a user such as:

class doctor (models.Model): user name = model.Agariniki (user, exclusive = true) ...

In the admin site, add a new doctor At the time, I have the option to add a new user next to the username. This is what I want, but what opens in that dialog asks the user name and password for the new user; I would also like to be able to assign groups to this new user. What would be the best way to do this?

Thank you, Lakharie

Your application includes an 'admin.py' Create, where you can actually register your own model with the administrator, add the following code. Import from django.contrib.auth.models user from

  django.contrib Auth.admin import userjmin.contrib.auth.forms Import UserCreationForm class MyUserCreationForm (UserCreationForm): "" is a form UserCreationForm "" class override meta: model = user field = ("user name", "group") UserAdmin.add_form = MyUserCreationForm admin.site.register (doctor)  

Override on the template that present this override form. Create a directory structure,

  "your_project_root_directory" /templates/admin/auth/user/add_form.html  

and copy the content,

  {% extended "admin / change_form.html"%} {% load i18n%} {% block after_field_sets%} & lt; P & gt; {% Trans "First, enter the username and password. You will be able to edit more user options."%} & Lt; / P & gt; & Lt; Fieldet class = "module align" & gt; & Lt; Div class = "form-line" & gt; Get the required class for {{Form.username.errors}} {# TODO: label_tag #} & lt; Label = "id_username" class = "required" & gt; {% Trans 'username'%}: & lt; / Label & gt; {{Form.username}} & lt; P class = "help" & gt; {{Form.username.help_text}} & lt; / P & gt; & Lt; / Div & gt; & Lt; Div class = "form-line" & gt; Get the required class for {{Form.password1.errors}} {# TODO: label_tag #} & lt; Label = "id_password1" class = "required" & gt; {% Trans 'Password'%}: & lt; / Label & gt; {{Form.password1}} & lt; / Div & gt; & Lt; Div class = "form-line" & gt; Get the required class for {{Form.password2.errors}} {# TODO: label_tag #} & lt; Label = "id_password2" class = "required" & gt; {% Trans 'password (again)'%}: & lt; / Labels & gt; {{Form.password2}} & lt; P class = "help" & gt; For verification {% trans}, enter the same password as above. ' %} & Lt; / P & gt; & Lt; / Div & gt; & Lt; Div class = "form-line" & gt; Get the required class at {{Form.groups.errors}} {# TODO: label_tag #} & lt; Label = "id_groups" class = "required" & gt; {% Trans 'groups'%}: & lt; / Label & gt; {{Form.groups}} & lt; P class = "help" & gt; {% Trans} All existing groups listed here If you are not looking at any group, then this means that you do not have any group object. '%} & Lt; / P & gt; & Lt; / Div & gt; & Lt; Script type = "text / javascript" & gt; Document.getElementById ("id_username") Focus (); & Lt; / Script & gt; & Lt; / Fieldset & gt; {% Endblock%}  

And you're good to go to snippets its job.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -