UserProfile StackedInLine/TabularInLine 重新设计

发布于 2024-08-03 15:57:29 字数 1531 浏览 16 评论 0原文

我最近扩展了 UserProfile,所以我的 admin.py 看起来像这样:

from django.contrib import admin
from django.contrib.auth.models import User
from models import UserProfile
from django.contrib.auth.admin import UserAdmin

admin.site.unregister(User)

class UserProfileInline(admin.StackedInline):
                model = UserProfile

class UserProfileAdmin(UserAdmin):
#               fieldsets = [
#                       (None,  {'fields': ['image']}),
#                       ('Avatar', {'fields': ['text'], 'classes': ['collapse']}),
#               ]
                inlines = (UserProfileInline,)

admin.site.register(User, UserProfileAdmin)

和 models.py 看起来像这样:

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

class UserProfile(models.Model):
        # Required
        user = models.ForeignKey(User, unique=True)
        image = models.ImageField(upload_to='media/users/', blank=True, help_text="Your face")
        text = models.TextField(blank=True, help_text="Write something about yourself")

In an app called users that is referred to by settings.py with:

    AUTH_PROFILE_MODULE = users.UserProfile

1

基本上我想要实现的是摆脱 #1 StackedInLine 这显示在管理中。我使用 StackedInLine 而不是的原因 TabularInLine 是因为否则我会得到“删除?”列到 是的,我发现它是可选的,所以我想排除它或 去掉 StackedInLine 中的 #1 编号。

2

还有。我想知道为什么我加载后无法使用字段集 admin.py 中的 UserProfile models.py 文件。简单的说就是字段 不存在。我是否必须以与 django/ 中不同的方式调用字段 contrib/auth/admin.py 我在哪里看到它工作?

如果您觉得有更有效的方法可以做到这一点,请告诉我。

I recently extended with UserProfile so my admin.py looks like this:

from django.contrib import admin
from django.contrib.auth.models import User
from models import UserProfile
from django.contrib.auth.admin import UserAdmin

admin.site.unregister(User)

class UserProfileInline(admin.StackedInline):
                model = UserProfile

class UserProfileAdmin(UserAdmin):
#               fieldsets = [
#                       (None,  {'fields': ['image']}),
#                       ('Avatar', {'fields': ['text'], 'classes': ['collapse']}),
#               ]
                inlines = (UserProfileInline,)

admin.site.register(User, UserProfileAdmin)

and models.py like this:

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

class UserProfile(models.Model):
        # Required
        user = models.ForeignKey(User, unique=True)
        image = models.ImageField(upload_to='media/users/', blank=True, help_text="Your face")
        text = models.TextField(blank=True, help_text="Write something about yourself")

In an app called users that is referred to by settings.py with:

    AUTH_PROFILE_MODULE = users.UserProfile

1

Basically what I want to achieve is to get rid of the #1 StackedInLine
that shows in the admin. The reason I use StackedInLine instead of
TabularInLine is because otherwise I get a "Delete?" column to the
right and I find it optional so I would like to either exclude that or
get rid of the #1 numbering in StackedInLine.

2

Also. I wonder why I cannot use fieldsets when I have loaded the
UserProfile models.py file in admin.py. It simply says the field
doesn't exist. Do I have to call the fields differently than in django/
contrib/auth/admin.py where I've seen it work?

If you feel like there is a more efficient way of doing this just tell me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

眼泪也成诗 2024-08-10 15:57:29

1

我认为你在这里太挑剔了。如果您绝对需要控制如此微小的细节,您应该创建自己的视图而不是使用管理员。否则,堆叠就是您想要的,因为表格对于一对一关系没有多大意义。

2

我已经能够在用户配置文件中使用字段集。我的代码和你的代码之间的唯一区别似乎是我使用元组而不是字典。这是我的比较:

class UserProfileInline(admin.StackedInline):
    model = UserProfile

class MyUserAdmin(UserAdmin):
    inlines = [UserProfileInline]
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )
    exclude = ['user_permissions']

编辑

我刚刚快速检查了一下,“#1”来自管理模板。

这意味着您可以通过覆盖库存管理模板轻松删除它,尽管这会影响您的所有内联,包括一对多的内联。

堆叠内联模板可以在 django/contrib/admin/templates/admin/edit_inline/stacked.html 中找到,

这意味着您可以将模板复制到您自己的模板目录 templates/admin/ edit_inline/stacked.html 这将由 Django 在运行时加载,而不是库存模板。

复制后,编辑本地副本以删除第 9 行的 #{{ forloop.counter }}

1

I think you're getting too picky here. If you absolutely need control over such minute details you should create your own views instead of using the admin. Otherwise stacked is what you want because tabular doesn't make much sense for one-to-one relations.

2

I've been able to use fieldsets in user profiles. The only difference between my code and yours seems to be that I'm using tuple's instead of dict's. Here's mine for comparison:

class UserProfileInline(admin.StackedInline):
    model = UserProfile

class MyUserAdmin(UserAdmin):
    inlines = [UserProfileInline]
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )
    exclude = ['user_permissions']

EDIT:

I just did a quick check and the "#1" is coming from the admin template.

This means you can easily remove it by overriding the stock admin template, although this will affect all your inlines including ones that are one-to-many.

The stacked inline template can be found in django/contrib/admin/templates/admin/edit_inline/stacked.html

This means you can copy the template to your own templates directory as templates/admin/edit_inline/stacked.html and this will be loaded by Django at run time instead of the stock template.

After copying edit your local copy to remove #{{ forloop.counter }} on line 9.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文