0

我正在为这样的人建模:

class Person(models.Model):
    """ Represent a person who has credentials. The person may have
    devices and/or accessories. """ 

    #basic information
    name = models.CharField(max_length=50, unique=True)
    email = models.CharField(max_length=50, unique=True)
    phone_number = PhoneNumberField(unique=True)

但我希望能够添加如下内容:

/* create admin user with a group priveledge*/
user_account = #link to user account here

我这样做是因为当我添加人员对象时,我希望他们拥有一个具有某些特权的帐户。

感谢任何能提供帮助的人..

4

2 回答 2

1

它全局取决于您使用的 Django 版本,因为用户模型和继承随着 Django 1.5 发生了变化。

您可能想看看 Django 官方文档:https ://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model

干杯,K。

编辑 19:30 CEST:您应该注意 Django 文档网站右上角的开关,以确保您查看的是正确的文档(即与您正在使用的 Django 版本有关的文档) . 从 1.5 开始,Django 添加了一个很棒的东西:不仅与 User 模型“相关”,而且还扩展了它。https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model

而且,我猜你想通过自动管理员添加用户,你也应该注意这一段:https ://docs.djangoproject.com/en/dev/topics/auth/customizing/#custom-users-和-django-contrib-admin

唯一的问题是您必须运行 Django 1.5+ 才能做到这一点。Django 1.4 和更低版本只允许您添加相关模型,这还不算太糟糕:)。

于 2013-05-07T16:42:40.003 回答
0

Can we say that what you want is just a User with a few more informations ?

Or is there a situation where you'll say

  • "A person may not be a user" and/or
  • "A user may not be a person"

What you describe here is possible with the simple system of the foreign key. Make a new model Person like this :

class Person(models.Model):
  ... some infos here ...
  user = model.ForeignKey(User)

And then, you'll just have to create the User and then, the Person. There's many ways you can tell Django to create automatically the Person object when the User object is created. This blogpost could drive you the right way : http://www.turnkeylinux.org/blog/django-profile

Maybe you should follow it, and then explain us exactly what you miss ?

Cheers, again.

K.

于 2013-05-07T18:38:35.737 回答