在django-admin中添加用户时分配组 [英] Assigning a group while adding user in django-admin

查看:264
本文介绍了在django-admin中添加用户时分配组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一些型号的用户模型作为外键,例如:

In my application, I have models that have the Users model as a Foreign key, eg:

class Doctor(models.Model):    
username=models.ForeignKey(User, unique=True)
...

在管理员网站中,添加新博士时,我可以选择在用户名字段旁添加新用户。这正是我想要的,但在打开的对话框中,它要求新用户的用户名和密码;我也想为这个新用户分配一个组。这样做最好的方法是什么?

In the admin site, when adding a new Doctor, I have the option of adding a new User next to the username field. This is exactly what I want, but in the dialog that opens, it asks for a username and password for the new user; I would also like to be able to assign a group to this new user. What would be the best way of doing this ?

谢谢,
lokharkey

Thanks, lokharkey

推荐答案

在您的应用程序中,创建一个admin.py,您可以在其中注册自己的模型给admin,添加以下代码。

In your application, create an 'admin.py', where you actually register your own model to admin, add following code.

from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm

class MyUserCreationForm(UserCreationForm):
    """
    A form that overrides the UserCreationForm
    """
    class Meta:
        model = User
        fields = ("username", "groups")

UserAdmin.add_form = MyUserCreationForm

admin.site.register(Doctor)

现在,您只需要覆盖渲染此覆盖表单的模板。
创建目录结构为

Now you just need to override the template that render this overridden form. Create a directory structure as,

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

并将内容复制为

{% extends "admin/change_form.html" %}
{% load i18n %}

{% block after_field_sets %}

<p>{% trans "First, enter a username and password. Then, you'll be able to edit more user options." %}</p>

<fieldset class="module aligned">

<div class="form-row">
  {{ form.username.errors }}
  {# TODO: get required class on label_tag #}
  <label for="id_username" class="required">{% trans 'Username' %}:</label> {{ form.username }}
  <p class="help">{{ form.username.help_text }}</p>
</div>

<div class="form-row">
  {{ form.password1.errors }}
  {# TODO: get required class on label_tag #}
  <label for="id_password1" class="required">{% trans 'Password' %}:</label> {{ form.password1 }}
</div>

<div class="form-row">
  {{ form.password2.errors }}
  {# TODO: get required class on label_tag #}
  <label for="id_password2" class="required">{% trans 'Password (again)' %}:</label> {{ form.password2 }}
  <p class="help">{% trans 'Enter the same password as above, for verification.' %}</p>
</div>

<div class="form-row">
  {{ form.groups.errors }}
  {# TODO: get required class on label_tag #}
  <label for="id_groups" class="required">{% trans 'Groups' %}:</label> {{ form.groups }}
  <p class="help">{% trans 'All existing Groups listed here. If you are not seeing any group, means you dont have any groups object created.' %}</p>
</div>

<script type="text/javascript">document.getElementById("id_username").focus();</script>

</fieldset>
{% endblock %}

你很好。其工作片段。

这篇关于在django-admin中添加用户时分配组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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