Django 扩展用户模型,需要对扩展模型进行额外的内联编辑
我用“会员”模型扩展了 django 用户模型。我希望能够(在管理站点上)管理用户 - 来自两个模型的所有数据。我需要用户的电子邮件和姓名,但其他所有内容都来自“会员”。我已经设置了它,以便我可以在一个表单上获取所有数据,但我无法继续将相关(子)信息添加到“成员”,就像我通常会做的那样,如果它没有显示为用户的内联模型。这是我这样做的尝试。目前它可以显示用户+会员,但无法显示销售商品(针对会员)。
from DjangoSite.ManageProducts.models import Member, SaleItem
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.admin import User
# cannot view this data from SaleItem model
class MemSalesInline(admin.TabularInline):
model = SaleItem
fk_name = 'Seller'
class MemberProfileInline(admin.StackedInline):
model = Member
fk_name = 'user'
# this does not work
inlines = [
MemSalesInline,
]
class MyUserAdmin(UserAdmin):
inlines = [MemberProfileInline,]
try:
admin.site.unregister(User)
except admin.sites.NotRegistered:
pass
admin.site.register(User, MyUserAdmin)
I have extended the django user model with a "Member" model. I would like to be able to administer (on admin site) the user - all data from both models. I need the email and name from User, but everything else is from "Member". I have set it up so that I can get all the data on one form, but I cannot continue to add related (child) information to the "Member" as I typically would if it was not being displayed as an inline model of user. Here is my attempt at doing this. It currently works to show User + Member, but cannot show SaleItems (for a Member).
from DjangoSite.ManageProducts.models import Member, SaleItem
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.admin import User
# cannot view this data from SaleItem model
class MemSalesInline(admin.TabularInline):
model = SaleItem
fk_name = 'Seller'
class MemberProfileInline(admin.StackedInline):
model = Member
fk_name = 'user'
# this does not work
inlines = [
MemSalesInline,
]
class MyUserAdmin(UserAdmin):
inlines = [MemberProfileInline,]
try:
admin.site.unregister(User)
except admin.sites.NotRegistered:
pass
admin.site.register(User, MyUserAdmin)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个很棒的教程描述了如何执行此操作:
http://www.b- list.org/weblog/2006/jun/06/django-tips-extending-user-model/
基本上,您可以使用 AUTH_PROFILE_MODULE 来连接您的 User 和 Member 模型。示例代码调用扩展的用户模型 UserProfile,您可能也想考虑这样做。
以下是教程中的代码:
您现在可以按如下方式访问扩展用户模型:
“内置用户模型上有一个名为 get_profile() 的方法,该方法可关闭此设置;如果定义了 AUTH_PROFILE_MODULE,则调用 get_profile()任何用户都会从您的自定义类返回关联的对象。”
There a great tutorial that describes how to do this:
http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/
Basically you can use the AUTH_PROFILE_MODULE to connect your User and Member models. The sample code calls the extended User model UserProfile, which you may want to consider doing as well.
Here's the code from the tutorial:
You can now access your extended user model as follows:
"There’s a method on the built-in User model called get_profile(), which keys off this setting; if AUTH_PROFILE_MODULE is defined, then calling get_profile() on any User will return the associated object from your custom class."
我找到了一个解决方案,我可以通过 User.email、user.first_name 或 user.last_name 搜索我的会员并获取会员记录 + 子记录 - 并且如果需要,仍然可以轻松返回用户记录。我遇到的问题是我需要能够按用户字段找到我的会员。我没有修改用户管理站点(django 的 auth_user),而是修改了我的会员管理站点(会员是 django 用户的扩展)。
admin.py
models.py
I have found a solution where I can search for my Members by User.email, user.first_name, or user.last_name and get back Member records + child records - and still easily get back to the User record if I need to. The issue I was having was that I needed to be able to find my Members by User fields. Instead of modifying the User admin site (django's auth_user), I modified my Member admin site (Member being my extension of django's User).
admin.py
models.py