django:使用queryset作为modelform初始数据 [英] django: use a queryset as modelform initial data

查看:67
本文介绍了django:使用queryset作为modelform初始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个设置界面,该界面的工作方式是扫描已安装的应用程序中的设置文件夹,扫描设置文件,最后扫描ModelForms.

I'm making a settings interface which works by scanning for a settings folder in the installed applications, scanning for settings files, and finally scanning for ModelForms.

我现在处于最后一步.可以正确找到并加载表单,但是现在我需要提供初始数据.初始数据将从数据库中提取,并且您可以想象,它必须限于通过身份验证的用户(通过request.user.id).

I'm at the last step now. The forms are properly found and loaded, but I now need to provide the initial data. The initial data is to be pulled from the database, and, as you can imagine, it must be limited to the authenticated user (via request.user.id).

请记住,这都是动态完成的.没有任何名称,其结构也不是高级已知的(我真的不想维护无聊的设置界面).

Keep in mind, this is all done dynamically. None of the names for anything, nor their structure is known in advanced (I really don't want to maintain a boring settings interface).

这是示例设置表格.我只是选择模型以及用户可以编辑的字段(这是我想要维护设置界面的程度).

Here is an example settings form. I just pick the model and which fields the user can edit (this is the extent to which I want to maintain a settings interface).

class Set_Personal_Info(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('nick_name', 'url')

我已经看过了modelformset_factory,它几乎完成了我想做的事情,但似乎只能处理两个或两个以上的结果.(在这里, obj 是设置形式之一)

I've looked at modelformset_factory which almost does what I want to do, but it only seems to work with results of two or more. (Here, obj is one of the settings forms)

Formset = modelformset_factory(obj.Meta.model, form=obj)
Formset(queryset=obj.Meta.model.objects.filter(id=request.user.id))

我无法过滤数据,我必须得到一个,只有一个结果.不幸的是我不能使用get()

I can't filter the data, I have to get one, and only one result. Unfortunately I can't use get()

Formset = modelformset_factory(obj.Meta.model, form=obj)
Formset(queryset=obj.Meta.model.objects.get(id=request.user.id))

用户"对象没有已排序"属性

'User' object has no attribute 'ordered'

由于不是列表,因此无法将查询结果提供为初始数据.

Providing the query result as initial data also doesn't work as it's not a list.

Formset = modelformset_factory(obj.Meta.model, form=obj)
Formset(initial=obj.Meta.model.objects.get(id=request.user.id))

用户"对象不支持索引

'User' object does not support indexing

我觉得答案就在我眼前.如何从数据库中提取数据库并将其作为初始值插入到表单中?

I have a feeling that the answer is right in front of me. How can I pull database from the database and shove it into the form as initial values?

推荐答案

我不太确定我是否了解您要执行的操作-如果您只对单一表单感兴趣,不知道为什么您完全参与了表单集.

I'm not really sure I understand what you're trying to do - if you're just interested in a single form, I don't know why you're getting involved in formsets at all.

要使用数据库中的初始数据填充模型表单,只需传递实例参数:

To populate a modelform with initial data from the database, you just pass the instance argument:

my_form = Set_Personal_Info(instance=UserProfile.objects.get(id=request.user.id))

在POST上实例化表单时,请不要忘记也传递实例参数,以便Django更新现有实例,而不是创建一个新实例.

Don't forget to also pass the instance argument when you're instantiating the form on POST, so that Django updates the existing instance rather than creating a new one.

(注意,您可能要考虑给对象起更好的名称. obj 通常描述的是模型实例,而不是表单,对于

form 更好的名称.表单类应遵循PEP8,并且可能包含单词"form"-因此 PersonalInfoForm 将是一个好名字.)

(Note you might want to think about giving better names to your objects. obj usually describes a model instance, rather than a form, for which form would be a better name. And form classes should follow PEP8, and probably include the word 'form' - so PersonalInfoForm would be a good name.)

这篇关于django:使用queryset作为modelform初始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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