django createview如何获取创建的对象 [英] django createview how to get the object that is created

查看:295
本文介绍了django createview如何获取创建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个连接的形式。基本上用户填写第一个表单,然后重定向到第二个表单的数据的第二个值。
E.G.我有一个表单电影(第一个表格),然后我被重定向到添加演员到电影的表单(演员)。



在我的情况下电影= Chiamata和Actor = Offerta(我保留意大利人的名字,我需要:D)



罚款。



这些是我的urls在urls.py

  url(r'^ chiamata / $',ChiamataCreate.as_view() name ='chiamata_create'),
url(r'^ chimamata /(?P< pk> \d +)/ offerta $',OffertaCreate.as_view(),name ='offerta_create'),

我有这个创建视图

 code>类ChiamataCreate(CreateView):
template_name ='chiamata_form.html'
form_class = ChiamataForm
success_url = reverse_lazy('offerta_create',args =(??,))

现在的问题是如何获得由chiamataForm创建的对象的PK。我需要将其添加到offerta_create的url。

解决方案

也许你可以使用 get_success_url )方法(请参阅参考资料



在这种情况下,它将像:



pre> def get_success_url(self):
return reverse('offerta_create',args =(self.object.id,))


i've two concatenated form. Basically user fills in the first form and then is redirected to the second one which adds value to the data of the first form. E.G. I've a form Movie (first form) and then i'm redirected to the form (actor) which add the actor to the movie.

in my case the Movie = Chiamata and Actor = Offerta (i keep the italians name for what i need :D)

fine.

those are my urls in the urls.py

url(r'^chiamata/$', ChiamataCreate.as_view(),name='chiamata_create'),
url(r'^chimamata/(?P<pk>\d+)/offerta$', OffertaCreate.as_view(), name='offerta_create'),

i've this create view

class ChiamataCreate(CreateView):
    template_name = 'chiamata_form.html'
    form_class = ChiamataForm
    success_url=reverse_lazy('offerta_create',args=(??,))

now the problem is how i can get the PK of the object created by the chiamataForm. I need that to add it to the the url of offerta_create.

解决方案

maybe you could use get_success_url() method (see reference)

In this case, it'd be something like:

def get_success_url(self):
    return reverse('offerta_create',args=(self.object.id,))

这篇关于django createview如何获取创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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