Django项目结构,推荐结构共享扩展认证“用户”跨应用的模型? [英] Django Project structure, recommended structure to share an extended auth "User" model across apps?

查看:79
本文介绍了Django项目结构,推荐结构共享扩展认证“用户”跨应用的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当用户模型扩展/分类和共同的用户模型在多个应用程序之间共享和使用时,通用项目/应用程序结构是什么。



我想在多个应用程序中引用相同的用户模型。
我还没有建立登录界面,所以我不知道它应该如何组合在一起。



以下是介绍:

  project.loginapp.app1 
project.loginapp.app2

这种情况有共同的模式吗?
最好使用登录应用程序进行登录?



与此问题类似,但更为具体。
django应用程序配置



更新



澄清我上面的用例。
我想向现有的auth用户模型添加字段(扩展或子类?)。然后在多个应用程序中引用该模型。

解决方案

为什么要扩展用户?请澄清。



如果您要添加有关用户的更多信息,则不需要滚动自己的用户和验证系统。 Django的版本是相当稳固的。用户管理位于django.contrib.auth中。



如果您需要自定义用户存储的信息,请先定义一个模型,如

  class Profile(models.Model):
...
user = models.ForeignKey(django.contrib.auth。 model.User,unique = True)

然后设置

  AUTH_PROFILE_MODULE =appname.profile

在您的settings.py中



设置此功能的优点是可以在您的视图中使用这样的代码:

  def my_view(request):
profile = request.user.get_profile()
etc ...
/ pre>

如果您想为用户提供更多的方式进行身份验证,则可以添加一个验证后端。扩展或重新实现django.contrib.auth.backends.ModelBackend并将其设置为
您的settings.py中的AUTHENTICATION_BACKENDS。



如果要使用与django提供的不同的权限或群组概念,没有什么会阻止你。 Django仅在django.contrib.admin(我知道)中使用这两个概念,你可以自由地为这些主题使用一些其他的概念,你认为合适。


I'm wondering what the common project/application structure is when the user model extended/sub-classed and this Resulting User model is shared and used across multiple apps.

I'd like to reference the same user model in multiple apps. I haven't built the login interface yet, so I'm not sure how it should fit together.

The following comes to mind:

project.loginapp.app1
project.loginapp.app2

Is there a common pattern for this situation? Would login best be handled by a 'login app'?

Similar to this question but more specific. django application configuration

UPDATE

Clarified my use-case above. I'd like to add fields (extend or subclass?) to the existing auth user model. And then reference that model in multiple apps.

解决方案

Why are you extending User? Please clarify.

If you're adding more information about the users, you don't need to roll your own user and auth system. Django's version of that is quite solid. The user management is located in django.contrib.auth.

If you need to customize the information stored with users, first define a model such as

class Profile(models.Model):
    ...
    user = models.ForeignKey("django.contrib.auth.models.User", unique=True)

and then set

AUTH_PROFILE_MODULE = "appname.profile"

in your settings.py

The advantage of setting this allows you to use code like this in your views:

def my_view(request):
    profile = request.user.get_profile()
    etc...

If you're trying to provide more ways for users to authenticate, you can add an auth backend. Extend or re-implement django.contrib.auth.backends.ModelBackend and set it as your AUTHENTICATION_BACKENDS in settings.py.

If you want to make use of a different permissions or groups concept than is provided by django, there's nothing that will stop you. Django makes use of those two concepts only in django.contrib.admin (That I know of), and you are free to use some other concept for those topics as you see fit.

这篇关于Django项目结构,推荐结构共享扩展认证“用户”跨应用的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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