Django 管理站点:“添加用户”页面如何工作(更多字段正在编辑)?

发布于 2024-09-27 12:54:05 字数 226 浏览 4 评论 0原文

我想知道他们如何能够在 Django 管理站点的用户页面中显示更多字段。 如果您创建一个新用户,您只有一些基本字段需要填写,但是如果您重新打开该用户(编辑模式),那么您会看到更多需要填写的字段。

我正在尝试实现相同的目标,我看了一下在 add_form.html 模板中,但我无法真正理解它。我想我正在寻找一种根据文档的编辑状态指定不同 fields = [] 集的方法。

谢谢!

I was wondering how they made it possible to display more fields in the User page of the Django admin site.
If you create a new User you only have some basic fields to fill in, but if you reopen that user (edit mode) then you see a lot more fields to fill in.

I'm trying to achieve the same, I had a look at the add_form.html template but I can't really get my head around it. I guess I'm looking for a way of specifying different fields = [] sets based on the edit status of the document.

Thanks!

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

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

发布评论

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

评论(2

梦情居士 2024-10-04 12:54:05

答案在于 为 User 模型注册的自定义管理类。它重写了 ModelAdmin 上的几个方法,并检查当前请求是否正在创建新的用户(在这种情况下,使用用于添加帐户的基本表单类) )或编辑现有表单(在这种情况下会显示完整的表单)。

The answer lies in the custom admin class registered for the User model. It overrides a couple of methods on ModelAdmin and checks to see whether the current request is creating a new User (in which case the bare-bones form class for adding accounts is used) or editing an existing one (in which case a full form is shown).

风吹短裙飘 2024-10-04 12:54:05

这是我的尝试。当我尝试创建新项目(添加)时,它仅显示某些字段,但当我点击保存时,它会返回错误:

DoesNotExist

在 /Library/Python/2.6/site-packages/django/db/models/fields/lated.py 中在 get 中,第 288 行

admin.py

    from django.contrib import admin
    from myapp.catalog.models import Model
    from myapp.catalog.forms import ProductAdminForm, ProductAddForm

    class ProductAdmin(admin.ModelAdmin):

        form = ProductAdminForm

        #...

        add_form = ProductAddForm

        def get_form(self, request, obj=None, **kwargs):
            defaults = {}
            if obj is None:
                defaults.update({
                    'form': self.add_form,
                })
            defaults.update(kwargs)
            return super(ProductAdmin, self).get_form(request, obj, **defaults)

forms.py

from myapp.catalog.models import Product

class ProductAdminForm(forms.ModelForm):

    class Meta:
        model = Product
        #...

class ProductAddForm(forms.ModelForm):
    class Meta:
      model = Product
      fields = ("model", "colour",)

Here's my try. When I try to create a new item (Add) it shows only certain fields but then when I hit save it returns an error:

DoesNotExist

in /Library/Python/2.6/site-packages/django/db/models/fields/related.py in get, line 288

admin.py

    from django.contrib import admin
    from myapp.catalog.models import Model
    from myapp.catalog.forms import ProductAdminForm, ProductAddForm

    class ProductAdmin(admin.ModelAdmin):

        form = ProductAdminForm

        #...

        add_form = ProductAddForm

        def get_form(self, request, obj=None, **kwargs):
            defaults = {}
            if obj is None:
                defaults.update({
                    'form': self.add_form,
                })
            defaults.update(kwargs)
            return super(ProductAdmin, self).get_form(request, obj, **defaults)

forms.py

from myapp.catalog.models import Product

class ProductAdminForm(forms.ModelForm):

    class Meta:
        model = Product
        #...

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