django 1.5 extend the default User model or substi

2020-03-26 12:11发布

问题:

Env: Django 1.5.1 + Django CMS 2.4.1 + Zinnia latest + my custom apps + custom Django CMS plugin

Basically I can extend the default Django (1.5.X) User model like Django Ribbit Tutorial on NetTuts+

or

Substitute a completely customized model like Django Dev doc or Django2Scoops example in the paragraph: "Dealing With the User Model"

To test I decided for Subclass AbstractUser From Django2Scoops book:"Choose this option if you like Django’s User model fields the way they are, but need extra ..fields."

But I have the following errors:

notification.noticesetting: 'user' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL. cmsplugin_zinnia.latestentriesplugin: 'authors' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL.

cms.pageuser: 'created_by' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL. cms.pageusergroup: 'created_by' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL.

After hours of reading and test I found

Specifying custom User model (extends AbstractUser) doesn't work

As the error message says -- you need to update the relation to point at settings.AUTH_USER_MODEL. The second error ("Model has been swapped out…") is a side effect of the fact that you're directly referencing the User model. Once you change the ForieignKey references, this third error will go away. We've done everything we can to ensure a smooth transition to the new User model, but it can't be completely transparent. App writers will need to update their apps to be 1.5 compatible. Essentially, a Django 1.4 app won't be 100% compatible with Django 1.5 if it contains a hard-coded foreign key reference to User. Please, could you give me more examples?

And Django/Python: Update the relation to point at settings.AUTH_USER_MODEL

in settings_example.py you have AUTH_USER_MODEL = 'users.User'. However you are using an app - menu.bookmark - that has a relation to django.contrib.auth.User - you can't have both. Setting AUTH_USER_MODEL means that you are replacing the built-in Django user model with your own. See http://procrastinatingdev.com/django/using-configurable-user-models-in-django-1-5/ for details.

But I don't understand how can I solve this.

What I would need:

-Users are linked to Institutes Class (one institute -> more users)

-Users or Institutes can have different permission and see different django cms pages/plugin.

-Several more fields for User.

Is Subclass AbstractUser the correct point?

How can I solve the "swapped out" error?

I should create something similar to OpenTreeMap Code

Isn't deprecated this code?

Thanks!

回答1:

There is very simple solution. Just need to register your custom user before importing CMSPlugin. Example:

from django.db import models
from django.contrib.auth import models as auth_models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
  telephone = models.CharField(max_length=100)
  email = models.CharField(max_length=100)

auth_models.User = User

from cms.models import CMSPlugin


回答2:

Here is my summary of the further discussion at https://github.com/divio/django-cms/issues/1798.

There are four potential options:

  1. If you need your custom user model to have a name other than User, you'll need to wait.
  2. You can call the custom user model User - though when I tried this, I got errors about clashes with related m2m fields. There are some further details on the above link which may help resolve this.
  3. Django 1.5 still lets you use user profiles. So if you are ok with using a deprecated feature, you can still use Django-CMS 2.4 and Django 1.5 with user profiles instead of a custom user model. (I misread the Django docs here and thought user profiles were not supported in Django 1.5.)
  4. You can often get away without either a user profile or a custom user model - they are best used to add data specifically for user authentication. Instead, you can use another model with a one-to-one relationship to User, and use the reverse relationship to access it.

For my case, I am going to go with #3 in the short-run and #4 in the long-run.

Hope that helps!