Django 中的内联表单集 - 删除某些字段

发布于 2024-07-14 07:26:26 字数 741 浏览 12 评论 0 原文

我需要创建一个内联表单集,其中

a)从完全显示的MyModel中排除一些字段

b)显示一些字段MyModel但阻止它们可编辑。

我尝试使用下面的代码,使用 values() 来将查询集过滤为我想要返回的值。 然而,这失败了。

有人有什么想法吗?

class PointTransactionFormset(BaseInlineFormSet):
    def get_queryset(self):
        qs = super(PointTransactionFormset, self).get_queryset()
        qs = qs.filter(description="promotion feedback")
        qs = qs.values('description','points_type') # this does not work
        return qs

class PointTransactionInline(admin.TabularInline):
    model = PointTransaction
    #formset = points_formset()
    #formset = inlineformset_factory(UserProfile,PointTransaction)
    formset = PointTransactionFormset

I need to create an inline formset which

a) excludes some fields from MyModel being displayed altogether

b) displays some some fields MyModel but prevents them from being editable.

I tried using the code below, using values() in order to filter the query set to just those values I wanted returned. However, this failed.

Anybody with any idea?

class PointTransactionFormset(BaseInlineFormSet):
    def get_queryset(self):
        qs = super(PointTransactionFormset, self).get_queryset()
        qs = qs.filter(description="promotion feedback")
        qs = qs.values('description','points_type') # this does not work
        return qs

class PointTransactionInline(admin.TabularInline):
    model = PointTransaction
    #formset = points_formset()
    #formset = inlineformset_factory(UserProfile,PointTransaction)
    formset = PointTransactionFormset

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

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

发布评论

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

评论(3

棒棒糖 2024-07-21 07:26:26

文档中似乎没有提到的一件事是,您可以在模型表单集的参数中包含表单。 因此,举例来说,假设您有一个人员模型表单,您可以通过执行此操作在模型表单集中使用它。

PersonFormSet = inlineformset_factory(User, Person, form=PersonForm, extra=6)

这允许您在模型表单级别上执行所有表单验证、排除等操作,并让工厂复制它。

One thing that doesn't seem to be said in the documentation is that you can include a form inside your parameters for model formsets. So, for instance, let's say you have a person modelform, you can use it in a model formset by doing this

PersonFormSet = inlineformset_factory(User, Person, form=PersonForm, extra=6)

This allows you to do all the form validation, excludes, etc on a modelform level and have the factory replicate it.

吲‖鸣 2024-07-21 07:26:26

这是在管理中使用的表单集吗? 如果是这样,只需设置“排除 = ['field1 ', 'field2']” 在 InlineModelAdmin 上排除字段。 要显示某些字段值不可编辑,您必须创建一个简单的自定义小部件,其 render() 方法仅返回值,然后重写 formfield_for_dbfield() 方法以将您的小部件分配给正确的字段。

如果这不是供管理员使用,而是供其他地方使用的表单集,那么您应该在传递给表单集构造函数的 ModelForm 子类中进行上述自定义(排除 Meta 内部类中的属性、 __init__ 方法中的小部件覆盖)。 (如果您使用的是 Django 1.2 或更高版本,则只需使用 readonly_fields 代替)。

如果您澄清您所处的情况(管理员或非管理员),我可以更新代码示例。

Is this a formset for use in the admin? If so, just set "exclude = ['field1', 'field2']" on your InlineModelAdmin to exclude fields. To show some fields values uneditable, you'll have to create a simple custom widget whose render() method just returns the value, and then override the formfield_for_dbfield() method to assign your widget to the proper fields.

If this is not for the admin, but a formset for use elsewhere, then you should make the above customizations (exclude attribute in the Meta inner class, widget override in __init__ method) in a ModelForm subclass which you pass to the formset constructor. (If you're using Django 1.2 or later, you can just use readonly_fields instead).

I can update with code examples if you clarify which situation you're in (admin or not).

樱花细雨 2024-07-21 07:26:26

我刚刚遇到了类似的问题(不适用于管理员 - 对于面向用户的网站),并发现您可以将要显示的表单集和字段传递到 inlineformset_factory 中,如下所示:

factory = inlineformset_factory(UserProfile, PointTransaction, 
                formset=PointTransactionFormset,
                fields=('description','points_type'))
formset = factory(instance=user_profile, data=request.POST)

其中 user_profile 是一个用户配置文件

请注意,如果基础模型具有未包含在传递到 inlineformset_factory 的字段列表中的必需字段,则这可能会导致验证问题,但任何类型的表单都是如此。

I just had a similar issue (not for admin - for the user-facing site) and discovered you can pass the formset and fields you want displayed into inlineformset_factory like this:

factory = inlineformset_factory(UserProfile, PointTransaction, 
                formset=PointTransactionFormset,
                fields=('description','points_type'))
formset = factory(instance=user_profile, data=request.POST)

where user_profile is a UserProfile.

Be warned that this can cause validation problems if the underlying model has required fields that aren't included in the field list passed into inlineformset_factory, but that's the case for any kind of form.

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