Django的内联管理员:“预填充"字段 [英] Django's Inline Admin: a 'pre-filled' field

查看:65
本文介绍了Django的内联管理员:“预填充"字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的第一个Django项目中,我希望用户能够在管理员中创建自定义表单,并向其中添加字段.为此,我在我的项目中添加了一个可重用的应用程序,该应用程序位于github上:

I'm working on my first Django project, in which I want a user to be able to create custom forms, in the admin, and add fields to it as he or she needs them. For this, I've added a reusable app to my project, found on github at:

https://github.com/stephenmcd/django-forms-builder

我遇到了麻烦,因为我想让它成为已创建的每种表单的1个特定字段为默认" ,因为在每种情况下都需要(通过我的方式,此处无关紧要,但此字段对应于地图上的一个点.

I'm having trouble, because I want to make it so 1 specific field is 'default' for every form that's ever created, because it will be necessary in every situation (by the way my, it's irrelevant here, but this field corresponds to a point on the map).

此django-forms-builder应用程序中的重要代码部分,显示了 admin.TabularInline 的使用:

An important section of code from this django-forms-builder app, showing the use of admin.TabularInline:

#admin.py
#...
class FieldAdmin(admin.TabularInline):
    model = Field
    exclude = ('slug', )


class FormAdmin(admin.ModelAdmin):
    formentry_model = FormEntry
    fieldentry_model = FieldEntry

    inlines = (FieldAdmin,)
#...

所以我的问题是:在最近的Django版本中,是否有任何简单的方法来为管理员"TabularInline"添加默认值(已填充字段)?到某个地方,我将学习如何解决这个问题.

So my question is: is there any simple way to add default (already filled fields) for an admin 'TabularInline' in the recent Django versions? If not possible, I would really appreciate a pointer to somewhere I could learn how to go about solving this.

重要提示:我在这里搜索了类似的问题,甚至用Google搜索了该问题.我发现了一些老问题(都是2014年或更早的版本),其中提到了Django无法直接提供这种可能性.假设我是Django的初学者,答案涉及一些复杂/令人困惑的建议.

Important: I've searched for similar questions here and even googled the issue. I've found some old questions (all from 2014 or way older) that mention this possibility not being directly provided by Django. The answers involved somewhat complex/confusing suggestions, given that I'm a Django begginer.

推荐答案

有两种方法可以实现.

1:在模型中的字段上设置默认值.

Django的动态管理表单和内联足够聪明,可以检测到并自动将其显示为默认选项或条目.

Django's dynamic admin forms and inlines are smart enough to detect this and display it automatically as a default choice or entry.

class Book(models.Model):

    rating = models.IntegerField(default=0)

2:在TabularInline类中使用自定义表单.

class BookInlineForm(models.ModelForm):

    class Meta:
        model = Book
        fields = ('rating', )

    def __init__(self, *args, **kwargs):
        initial = kwargs.pop('initial', {})
        # add a default rating if one hasn't been passed in
        initial['rating'] = initial.get('rating', 0)
        kwargs['initial'] = initial
        super(BookInlineForm, self).__init__(
            *args, **kwargs
        )


class BookTabularInline(admin.TabularInline):
    model = Book
    form = BookInlineForm


class ShelfAdmin(admin.ModelAdmin):
    inlines = (BookAdmin,)

这篇关于Django的内联管理员:“预填充"字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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