Django中的内联格式 - 删除某些字段 [英] Inline formset in Django - removing certain fields

查看:283
本文介绍了Django中的内联格式 - 删除某些字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



a)将 MyModel 中的某些字段排除在外, / p>

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



我尝试使用下面的代码,使用 values(),以便将查询集过滤到我想要返回的那些值。不过,这个失败了。



任何人有任何想法?

  class PointTransactionFormset(BaseInlineFormSet):
def get_queryset(self):
qs = super(PointTransactionFormset,self).get_queryset()
qs = qs.filter(description =促销反馈)
qs = qs.values('description','points_type')#这不工作
return qs

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


解决方案

文档中似乎没有说明的一件事是,您可以在模型表单的参数中包含一个表单。所以,例如,假设你有一个人模型,你可以通过这样做在模型表单中使用它

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

表单验证,排除等等,并在工厂复制它。


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

解决方案

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.

这篇关于Django中的内联格式 - 删除某些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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