Django 扩展用户模型,需要对扩展模型进行额外的内联编辑

发布于 2024-11-15 06:28:52 字数 915 浏览 9 评论 0原文

我用“会员”模型扩展了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄离 2024-11-22 06:28:52

有一个很棒的教程描述了如何执行此操作:
http://www.b- list.org/weblog/2006/jun/06/django-tips-extending-user-model/

基本上,您可以使用 AUTH_PROFILE_MODULE 来连接您的 User 和 Member 模型。示例代码调用扩展的用户模型 UserProfile,您可能也想考虑这样做。

以下是教程中的代码:

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    url = models.URLField()
    home_address = models.TextField()
    phone_numer = models.PhoneNumberField()
    user = models.ForeignKey(User, unique=True)

# In Settings.py

AUTH_PROFILE_MODULE = 'myapp.UserProfile'

您现在可以按如下方式访问扩展用户模型:

from django.contrib.auth.models import User
u = User.objects.get(pk=1)
user_address = u.get_profile().home_address

“内置用户模型上有一个名为 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:

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    url = models.URLField()
    home_address = models.TextField()
    phone_numer = models.PhoneNumberField()
    user = models.ForeignKey(User, unique=True)

# In Settings.py

AUTH_PROFILE_MODULE = 'myapp.UserProfile'

You can now access your extended user model as follows:

from django.contrib.auth.models import User
u = User.objects.get(pk=1)
user_address = u.get_profile().home_address

"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."

帅气尐潴 2024-11-22 06:28:52

我找到了一个解决方案,我可以通过 User.email、user.first_name 或 user.last_name 搜索我的会员并获取会员记录 + 子记录 - 并且如果需要,仍然可以轻松返回用户记录。我遇到的问题是我需要能够按用户字段找到我的会员。我没有修改用户管理站点(django 的 auth_user),而是修改了我的会员管理站点(会员是 django 用户的扩展)。

admin.py

from DjangoSite.ManageProducts.models import Member, SaleItem
from django.contrib import admin

class MemSalesInline(admin.TabularInline):
    model = SaleItem
    fk_name = 'Seller'

class MemberAdmin(admin.ModelAdmin):
        list_display = ['__str__', 'lname', 'fname'] # fname, lname not sortable
        raw_id_fields = ("user",)
        #this allows search "members" by User fields.
        search_fields = ['user__email', 'user__last_name', 'user__first_name']
    # include any child records here - child of "Member"
    inlines = [MemSalesInline,]

admin.site.register(Member, MemberAdmin)

models.py

class Member(models.Model):
    MemberID = models.AutoField(primary_key=True)
    Country = models.ForeignKey(dmCountry, blank=True, null=True)
    # plus more additional member info...
    # these allow me to view email, last name and first name in the Member admin list page.
    def __str__(self):
        return "%s" % self.user.email
    def lname(self):
        return "%s" % self.user.last_name
    def fname(self):
        return "%s" % self.user.first_name

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

from DjangoSite.ManageProducts.models import Member, SaleItem
from django.contrib import admin

class MemSalesInline(admin.TabularInline):
    model = SaleItem
    fk_name = 'Seller'

class MemberAdmin(admin.ModelAdmin):
        list_display = ['__str__', 'lname', 'fname'] # fname, lname not sortable
        raw_id_fields = ("user",)
        #this allows search "members" by User fields.
        search_fields = ['user__email', 'user__last_name', 'user__first_name']
    # include any child records here - child of "Member"
    inlines = [MemSalesInline,]

admin.site.register(Member, MemberAdmin)

models.py

class Member(models.Model):
    MemberID = models.AutoField(primary_key=True)
    Country = models.ForeignKey(dmCountry, blank=True, null=True)
    # plus more additional member info...
    # these allow me to view email, last name and first name in the Member admin list page.
    def __str__(self):
        return "%s" % self.user.email
    def lname(self):
        return "%s" % self.user.last_name
    def fname(self):
        return "%s" % self.user.first_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文