I'm using CBV in Django 1.9 and in CreateView when I try to pass an additional parameter ('pk') to my form using self.kwargs.pop('pk') i got "Key Error" but if I get the parameter by index it works, here is my code:
def get_form(self, form_class=None, **kwargs):
self.project_version_pk = self.kwargs.pop('pk')
form = super(HRCreateView, self).get_form(form_class)
form.fields['project_version'].queryset = form.fields['project_version'].queryset.filter(pk=self.project_version_pk)
form.fields['project_version'].initial = self.project_version_pk
return form
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class, pk=self.kwargs['pk'])
return self.render_to_response(
self.get_context_data(form=form)
And I get this error:
builtins.KeyError
KeyError: 'pk'
File "RelationView.py", line 65, in get_form
self.project_version_pk = self.kwargs.pop('pk')
KeyError: 'pk'
But if i read the key this way it works:
def get_form(self, form_class=None, **kwargs):
self.project_version_pk = self.kwargs['pk']
form = super(HRCreateView, self).get_form(form_class)
form.fields['project_version'].queryset = form.fields['project_version'].queryset.filter(pk=self.project_version_pk)
form.fields['project_version'].initial = self.project_version_pk
return form
I don't really understand why the parameter is missing on pop() or which is the best practice for this.
Firstly, you shouldn't be overriding get. In a CreateView, Django already calls get_form for you - inside get_context_data. This is the cause of the issue you are having; you call get_form and pop the pk so that it is no longer in kwargs; but Django calls it again in get_context_data, but this second time it can't find the pk because you removed it the first time.
So don't use pop; but, as I said, don't do this at all. The only thing you actually need to override is get_form.
Related
I want to use my own method, which will return JsonResponse, but this method isn't called in this View by default. So maybe i need to rewrite another method or?
views.py:
class CreatePostJS(LoginRequiredMixin, SelectRelatedMixin, generic.CreateView):
fields = ('post_message', 'post_image')
model = models.Post
select_related = ('user',)
template_name = 'posts/post_list.html'
template_name = 'posts/post_form.html'
response_data = {}
response_data['text'] = model.post_message
response_data['posted'] = model.posted_at
response_data['user'] = model.user
def create_post(request):
if request.method == 'POST':
return JsonResponse(serializers.serialize('json', response_data), safe = False)
def form_valid(self, form):
self.object = form.save(commit = False)
self.object.user = self.request.user
self.object.save()
return super().form_valid(form)
https://docs.djangoproject.com/en/3.1/topics/class-based-views/intro/
Basically if you want to execute the method on POST requests, you can override the post method with your own. It will look more or less like this:
def post(self, request, *args, **kwargs):
my_json_response = self.create_post(request)
return super().post(request, *args, **kwargs)
That way there is no need to check for request.method in create_post, unless you plan to use it elsewhere.
When you work with generic views and want to override the default request handler, you need to overwrite the default method handler for the request method. In your case, it's the post() method from CreateView class.
I strongly recommend OOP way, so it would look like:
def get_request_data(self):
return {
'text': self.model.post_message,
'posted': self.model.posted_at,
'user': self.model.user
}
def create_post(request):
return JsonResponse(serializers.serialize('json', self.get_response_data()), safe = False)
def post(self, request, *args, **kwargs):
return self.create_post(request)
Note the if request.method == 'POST': is not needed, as post() method will only execute when POST request is being made in the current view.
You tried to initialize response_data outside a method, so as in my code you'd need to create another method for this - which I included in my example code.
I have test like that:
def test_getting_delete_view_invokes_point_changes_and_returns_status_200(self, point_changes):
request = RequestFactory().get(reverse('questions:delete-help', kwargs={'pk': 1}))
view = DeleteHelp.as_view()
view.kwargs['pk'] = 1
response = view(request)
And my view function:
class DeleteHelp(DeleteView, LoginRequiredMixin):
model = Help
template_name = 'confirm_deletion.html'
def get_object(self, queryset=None):
return get_object_or_404(Help, pk=self.kwargs['pk'], author=self.request.user)
def get_success_url(self):
point_pk = self.object.answer_to.plan_point.point_of.id
point_changes(point_obj=self.object.answer_to.plan_point)
return reverse('plans:display-public', args=[point_pk])
The question is, how am I supposed to pass 'pk' there? I keep getting an error KeywordError 'pk' in get_object method. If I use self.client to access this view then it works (why?), but I want to use RequestFactory.
Any help would be appreciated.
You pass it when you call the view.
view = DeleteHelp.as_view()
response = view(request, pk=1)
I am trying to use a formset to create forms for a set of timeframes related to dates:
class Event(models.Model):
date = models.DateField()
class TimeFrame(models.Model):
start = models.DateTimeField()
end = models.DateTimeField()
event = models.ForeignKey('Event')
I have code that gets me a queryset of timeframes for each event and added a kwarg to pass this into my form:
class SelectDatesForm(forms.Form):
timeframes = forms.ModelChoiceField(queryset=HostTimeFrame.objects.none())
def __init__(self, *args, **kwargs):
qs = kwargs.pop('timeframes')
super(SelectDatesForm, self).__init__(*args, **kwargs)
self.fields['timeframes'].queryset = qs
Now I'm trying to construct a formset that lets me show timeframes for multiple events on one page. I already found this question, explaining how to pass initial data, for serveral forms, but its not the same as passing it to a queryset.
Also there is this new function from django 1.9 but it doesnt allow me to get different querysets for each form.
UPDATE:
I got the solution from the answer working, however, whenever im running formset.is_valid() i get the error:
Select a valid choice. That choice is not one of the available
choices.
Here is what I do in my view:
timeframes = [HostTimeFrame.objects.all()]
SelectDatesFormset = formset_factory(form=SelectDatesForm, extra=len(timeframes), formset=BaseSelectDatesFormSet)
if request.method == 'POST':
formset = SelectDatesFormset(request.POST, form_kwargs={'timeframes_list': timeframes})
if formset.is_valid():
# do something with the formset.cleaned_data
print(formset)
pass
else:
formset = SelectDatesFormset(form_kwargs={'timeframes_list': timeframes})
Ive been trying for hours to find where this actual validation is done, but i cant find it for the live of me.
Edit: I tested this with the singular form, and i have the same issue, I created a new question for this here.
UPDATE:
Only partial solution, see question.
Solved it myself:
First I created a BaseFormSet:
class BaseSelectDatesFormSet(BaseFormSet):
def get_form_kwargs(self, index):
kwargs = super(BaseSelectDatesFormSet, self).get_form_kwargs(index)
kwargs['timeframes'] = kwargs['timeframes_list'][index]
return kwargs
Then I could pass the list of timeframes in the view:
SelectDatesFormset = formset_factory(form=SelectDatesForm, extra=4, formset=BaseSelectDatesFormSet)
formset = SelectDatesFormset(form_kwargs={'timeframes_list': timeframes})
Finally I had to update my form init to pop the list as well so the super constructor doesnt complain about unwanted kwargs:
def __init__(self, *args, **kwargs):
qs = kwargs.pop('timeframes')
qs_list = kwargs.pop('timeframes_list')
super(SelectDatesForm, self).__init__(*args, **kwargs)
self.fields['timeframes'].queryset = qs.order_by('start')
For peeps using Class Based View FormView along with form_class as formset or formset_factory, they can add an extra attribute as follows:
Pass form_kwargs in the get_form method by overriding it.
timeframes = [HostTimeFrame.objects.all()]
class SelectDatesView(FormView):
form_class = formset_factory(form=SelectDatesForm, extra=len(timeframes)
def get_form(self, form_class=None):
"""Override the method to add form kwargs. Returns an instance of the form to be used in this view."""
if form_class is None:
form_class = self.get_form_class()
return form_class(**self.get_form_kwargs(), form_kwargs={"timeframes": timeframes})
One can access it directly in the __init__ method's kwargs.
def __init__(self, *args, **kwargs):
super(SelectDatesForm, self).__init__(*args, **kwargs)
qs = kwargs.get('timeframes')
self.fields['timeframes'].queryset = qs.order_by('start')
I have the following class based view that I want to use to render a formset and validate it when it gets submitted through a post method:
The formset renders perfectly. When I submit the form I can read the formset and check it for errors. in the post method of this class -> errors = backorder_formset.errors
If I find any errors in the formset, I would like to render the view, but this time with the formset instance, that I read from POST.
When I call ctx = self.get_context_data() form within the post method of the class the following error gets raised from the call super(MissingProductsListView, self).get_context_data(*args, **kwargs):
'MissingProductsListView' object has no attribute 'object_list'
It seems like the superclass of Listview performs this call:queryset = kwargs.pop('object_list', self.object_list)
My question is why am I running in this error? and how could I render this formset with its errors messages to display it in the template after it was posted? I am using Django 1.9.9
class MissingProductsListView(generic.ListView):
template_name = 'dashboard/purchaseorder/missing_products.html'
context_object_name = 'backorders'
model = BackOrder
def post(self, request, *args, **kwargs):
backorder_formset = BackOrderFormset(request.POST)
errors = backorder_formset.errors
if backorder_formset.is_valid():
# <process form cleaned data>
return HttpResponseRedirect('/success/')
else:
ctx = self.get_context_data()
return self.render_to_response(ctx)
def accumulate_identical_products_from_backorders(self, back_order_list):
... some code
return sorted_accumulated_dict.values()
def get_context_data(self, *args, **kwargs):
ctx = super(MissingProductsListView, self).get_context_data(*args, **kwargs)
ctx['title'] = _("Missing Products")
if self.request.POST:
ctx['back_order_formset'] = BackOrderFormset(self.request.POST)
else:
accumulated_backorders_per_product = self.accumulate_identical_products_from_backorders(BackOrder.objects.all())
back_orders = BackOrderFormset(initial=[{'product_id': backorder_dict['product_id'],
'product': backorder_dict['title'],
'quantity': backorder_dict['quantity']} for backorder_dict in
accumulated_backorders_per_product])
ctx['back_order_formset'] = back_orders
return ctx
def get_queryset(self):
.. some code
return backorder_list
Look here:
class BaseListView(MultipleObjectMixin, View):
"""
A base view for displaying a list of objects.
"""
def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
allow_empty = self.get_allow_empty()
if not allow_empty:
# When pagination is enabled and object_list is a queryset,
# it's better to do a cheap query than to load the unpaginated
# queryset in memory.
if self.get_paginate_by(self.object_list) is not None and hasattr(self.object_list, 'exists'):
is_empty = not self.object_list.exists()
else:
is_empty = len(self.object_list) == 0
if is_empty:
raise Http404(_("Empty list and '%(class_name)s.allow_empty' is False.") % {
'class_name': self.__class__.__name__,
})
context = self.get_context_data()
return self.render_to_response(context)
Basically - you missed this part in the POST handler:
self.object_list = self.get_queryset()
And to be honest - I am not quite sure if this is a good idea to add post to the generic ListView in django. It looks more like FormView - but I can be wrong here.
I have a mixin on top of my app/views.py:
class RefereeViewMixin(object):
#method_decorator(login_required)
def dispatch(self, *args, **kwargs):
if not self.request.user.is_superuser and \
not self.request.user.has_group("referee"):
raise PermissionDenied
return super(RefereeViewMixin, self).dispatch(*args, **kwargs)
And also a view which is derived from my custom mixin and CreateView:
class RefereeResultCreateView(RefereeViewMixin, CreateView):
category = None
template_name = "referee/result_create.html"
fields = ["minutes", "seconds", "miliseconds", "disqualification"]
def get_success_url(self):
return reverse("order_list", args=[self.category])
def get_context_data(self, **kwargs):
context = super(RefereeResultCreateView, self).get_context_data(
**kwargs)
context["project"] = Project.objects.get(pk=self.kwargs.get("pk"))
return context
def form_valid(self, form):
result = form.save(commit=False)
result.project = Project.objects.get(pk=self.kwargs.get("pk"))
result.save()
messages.success(self.request, _(
"Result entry for {} project created.".format(result.name)))
return super(RefereeResultCreateView, self).form_valid(form)
Finally, my final view which is derived from RefereeResultCreateView:
class RefereeFireFighterResultCreateView(RefereeResultCreateView):
model = FireFighterResult
category = "fire_fighter"
fields = RefereeResultCreateView.fields + [
"extinguish_success", "extinguish_failure", "wall_hit"]
And the pattern is like that:
url(r'^fire_fighter/(?P<pk>\d+)/create/$',
RefereeFireFighterResultCreateView.as_view(),
name="referee_fire_fighter_result_create")
However, when I go to /app/fire_fighter/some_id/create/ I got TypeError. It tells me super(type, obj): obj must be an instance or subtype of type. When I debug it, I see that RefereeResultCreateView's get_context_data method causes that error. If I don't override that method, I don't see any errors. When I override it at RefereeFireFighterResultCreateView class, I don't see any errors.
I can overcome the problem. However, I think it is not the elegant way. Actually, it is very dirty. Also, I am curious about the reason. Why it doesn't work in the way I expect? What is the elegant solution?
UPDATED
Here's the traceback:
https://gist.github.com/ilkerkesen/e65fefa66890758bcdcd