【发布时间】:2021-03-11 23:01:39
【问题描述】:
所以我有一个使用模型表单“A”的 CreateView,我还有一个不同的模型,当模型表单“A”中的这个模型被提交时,我想编辑它。
如何访问未创建的对象?有没有更好的办法?
这里是form_valid函数()
def form_valid(self, form):
member = self.object
form.instance.registration_upto = form.cleaned_data['registration_date'] + delta.relativedelta(
months=int(form.cleaned_data['subscription_period']))
if form.cleaned_data['fee_status'] == 'paid':
payments = Payments(
user=member,
payment_date=form.cleaned_data['registration_date'],
payment_period=form.cleaned_data['subscription_period'],
payment_amount=form.cleaned_data['amount'])
payments.save()
return super().form_valid(form)
Self.object 为 None,因为该对象尚未创建。我想访问该对象,因为它是我的付款模型中的用户。我只需要坚持基于类的视图。有什么办法可以做到吗?
我对 Django 有点陌生,查看文档并没有太大帮助。 感谢您的任何建议
已解决
【问题讨论】:
标签: django django-views