Ad

Django Project Structure, Recommended Structure To Share An Extended Auth "User" Model Across Apps?

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.

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad