[英]UpdateView not populating form with existing data
So I have my UpdateView set up to send a request
object to the form so I can modify a queryset in the form (based on request.user
)因此,我将 UpdateView 设置为向表单发送
request
对象,以便我可以修改表单中的查询集(基于request.user
)
my views.py:我的意见.py:
class DataSourceUpdateView(UpdateView):
model = DataSource
form_class = DataSourceForm
template_name = 'engine/datasource_update.html'
def get(self, request, *args, **kwargs):
obj = DataSource.objects.get(pk=kwargs['pk'])
self.object = None
form = DataSourceForm(request)
return self.render_to_response(
self.get_context_data(form=form,
object=obj))
def post(self, request, *args, **kwargs):
form = DataSourceForm(request, request.POST, request.FILES)
if form.is_valid:
return self.form_valid(form)
else:
return self.form_invalid(form)
my forms.py:我的forms.py:
class DataSourceForm(forms.ModelForm):
def __init__(self, request, *args, **kwargs):
self.request = request
super(DataSourceForm, self).__init__(*args, **kwargs)
self.fields['dataset_request'].queryset = DatasetRequest.objects.filter(
creator=self.request.user)
class Meta:
model = DataSource
exclude = ('creator', 'vote_score', 'num_vote_up',
'num_vote_down', 'file_size', 'slug')
My problem is, in the template, the form is not populated with existing values.我的问题是,在模板中,表单未填充现有值。 How can I fix this?
我怎样才能解决这个问题?
With UpdateView
it's a little bit tricky.使用
UpdateView
有点棘手。 So, in order to initialize your form's data, you need to do it in the view itself not in the form.因此,为了初始化表单的数据,您需要在视图本身而不是在表单中进行初始化。
So here is how you can perform what's you've done when using UpdateView
:因此,以下是您在使用
UpdateView
时如何执行已完成的UpdateView
:
class DataSourceUpdateView(UpdateView):
model = DataSource
form_class = DataSourceForm
template_name = 'engine/datasource_update.html'
# An empty dict or add an initial data to your form
initial = {}
# And don't forget your success URL
# or use reverse_lazy by URL's name
# Or better, override get_success_url() method
# And return your success URL using reverse_lazy
sucess_url = '/'
def get_initial(self):
"""initialize your's form values here"""
base_initial = super().get_initial()
# So here you're initiazing you're form's data
base_initial['dataset_request'] = DatasetRequest.objects.filter(
creator=self.request.user
)
return base_initial
#... The rest of your view logic
And you're form will be:你的形式将是:
class DataSourceForm(forms.ModelForm):
class Meta:
model = DataSource
exclude = (
'creator',
'vote_score',
'num_vote_up',
'num_vote_down',
'file_size',
'slug'
)
Bonus:奖金:
In order to understand why you need to initialize the form's data, you need to see the `UpdateView's MRO which are Visit this documentation link :为了理解为什么需要初始化表单的数据,您需要查看`UpdateView's MRO,请访问此文档链接:
And the FormMixin
have these attributes and methods visit the documentation link which are: FormMixin
具有这些属性和方法, 请访问文档链接,它们是:
Also i recommend you to see what the FormMixin
have like attributes and methods in order to see how you can override them or let Django do magics for you :D.此外,我建议您查看
FormMixin
具有的属性和方法,以便了解如何覆盖它们或让 Django 为您FormMixin
魔法:D。 See this documentation link请参阅此文档链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.