I want to be able to create a lorum, using a ipsum_id (ForeignKey) that has been supplied in the URL. Using Django's class based views.
Given the following URL:
url(r'^lorem/create/ipsum/(?P<ipsum_id>\d+)/$', LoremCreateView.as_view(), name='create'),
I have the following view:
class LoremCreateView(CreateView):
""" Allow a user to create a Lorem. """
model = Lorem
...
def get_context_data(self, **kwargs):
""" Get context variables for rendering the template. """
ipsum = get_object_or_404(Ipsum, pk=self.kwargs['ipsum_id'])
kwargs['ipsum'] = ipsum
return super().get_context_data(**kwargs)
def form_valid(self, form):
""" Save the form instance. """
ipsum = get_object_or_404(Ipsum, pk=self.kwargs['ipsum_id'])
form.instance.ipsum = ipsum
return super().form_valid(form)
This works fine, however I don't like the fact I call get_object_or_404 twice.
Is there a better way of doing this?
It might be possible to override an appropriate method and set self.ipsum - not sure I like doing this either.
Also is get_object_or_404 appropriate here?