Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

django createview how to get the object that is created

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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,))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...