django 1.5扩展默认用户模型或替换它 [英] django 1.5 extend the default User model or substitute it

查看:400
本文介绍了django 1.5扩展默认用户模型或替换它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Env:Django 1.5.1 + Django CMS 2.4.1 + Zinnia最新+我的自定义应用程序+自定义Django CMS插件



基本上我可以扩展默认的Django 1.5.X)用户模型
Django Ribbit教程在NetTuts +





替换完全自定义的模型,如 Django Dev doc 或Django2Scoops示例中的段落:处理用户模型



为了测试我决定了Subclass AbstractUser
从Django2Scoops的书:如果您喜欢Django的用户模型字段,请选择此选项,但是需要额外的..field。



但是我有以下错误:


notification.noticesetting:'user'定义与模型
的关系auth.User,已换出。在settings.AUTH_USER_MODEL中更新关键点
。 cmsplugin_zinnia.latestentries插件:
'authors'与模型'auth.User'定义了一个关系,其中
已被换出。更新关系以指向
settings.AUTH_USER_MODEL。



cms.pageuser:'created_by'定义与模型
'auth.User的关系,这已经被交换出去。在settings.AUTH_USER_MODEL中更新关键点
。 cms.pageusergroup:'created_by'与模型'auth.User'定义了一个
关系,它已被换出。
更新关系以指向设置.AUTH_USER_MODEL。


经过几个小时的阅读和测试,我发现



指定自定义用户模型(extends AbstractUser)不起作用


如错误消息所示,您需要将关系更新为点
在settings.AUTH_USER_MODEL。第二个错误(模型已被交换
out ...)是您直接引用
用户模型的事实的副作用。一旦更改了ForieignKey引用,这个第三个
错误就会消失。我们已经做了一切可以确保平滑的
转换到新的User模型,但它不能完全
透明。应用程序作者将需要更新他们的应用程序为1.5
兼容。基本上,如果Django 1.4应用程序包含硬编码的外键引用
用户,则Django 1.4应用程序将不会与Django 1.5兼容100%兼容。请你能给我更多的例子吗?


Django / Python:将关系更新为设置点.AUTH_USER_MODEL


在settings_example.py中,


您有AUTH_USER_MODEL ='users.User'。
但是,您正在使用一个与
django.contrib.auth.User有关系的应用程序 - menu.bookmark - 您不能同时拥有这两个。设置
AUTH_USER_MODEL意味着您正在用自己的代替内置的Django用户
模型。请参阅
http:// procrastinatingdev。 com / django / using-configured-user-models-in-django-1-5 /



$ b $但是我不明白如何解决这个问题。



我需要什么:



- 用户链接到研究所课程(一个学院 - >更多用户)



- 用户或研究所可以有不同的权限,看到不同的django cms
页面/



- 用户的多个字段。



子类AbstractUser是否正确?

我如何解决换出错误?



我应该创建类似于 OpenTreeMap代码



不是不赞成这个代码?



谢谢!

解决方案

有很简单的解决方案。只需要导入CMSPlugin之前注册您的自定义用户。示例:


 从django.db导入模型
from django.contrib.auth import模型作为auth_models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
phone = models.CharField(max_length = 100)
电子邮件= models.CharField(max_length = 100)

auth_models.User =用户
$ b从cms.models导入CMSPlugin



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!

解决方案

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

这篇关于django 1.5扩展默认用户模型或替换它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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