Passing kwargs to class based view in unittest - python

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)

Related

Own method in CreateView

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.

How to get url to action method from ModelViewSet(Django Rest Framework) with reverse function?

I have this ModelViewSet class:
class DriveInvoiceViewSet(viewsets.ModelViewSet):
filter_fields = ('location_id', 'logical_deleted')
permission_classes = (UserCanManageFacilityPermission,)
pagination_class = None
def get_serializer_class(self):
...
def get_queryset(self):
...
#action(detail=False, methods=['GET'])
def get_subtotals_by_unit(self, request):
invoices_list = self.filter_queryset(self.get_queryset())
grouped_invoices = get_subtotals_by_unit(invoices_list)
return Response(grouped_invoices)
How can I get the URL from reverse function to test the get_subtotals_by_unit action?
The ViewSet registred by the router router.register('drive_invoices', DriveInvoiceViewSet, base_name='drive_invoices')
Change the action decorator slightly as below,
class DriveInvoiceViewSet(viewsets.ModelViewSet):
# other code
#action(detail=False, methods=['GET'], url_path="/some/path/", url_name="some-view-name")
def get_subtotals_by_unit(self, request):
invoices_list = self.filter_queryset(self.get_queryset())
grouped_invoices = get_subtotals_by_unit(invoices_list)
return Response(grouped_invoices)
Thus, DRF will create a url pattern with a view name with a syntax as <router_base_name>-<action_view_name>
Thus, the view name in your case will be, drive_invoices-some-view-name

Calling some functions if Generic View is success

I use generic views/class-based views in my project and I want to call some functions if view is done successfully. I use for this def get_success_url() method. But I can't reach the model in that function. How can I get rid of this, or are there any other way to do this?
My codes:
class MyModelUpdate(UpdateView):
model = MyModel
fields = ['details']
def get_success_url(self, **kwargs):
add_log(form.model, 2, 1, request.POST.user)
return reverse_lazy('model-detail', kwargs = {'pk' : self.kwargs['model_id'] })
The UpdateView class implements UpdateMixin, which has methods such as form_valid, which is only called if the form data is valid.
So you could do:
class MyModelUpdate(UpdateView):
model = MyModel
fields = ['details']
def get_success_url(self, **kwargs):
return reverse_lazy('model-detail', kwargs={'pk': self.kwargs['model_id']})
def form_valid(self, form):
# The super call to form_valid creates a model instance
# under self.object.
response = super(MyModelUpdate, self).form_valid(form)
# Do custom stuff here...
add_log(self.object, 2, 1, self.request.POST.user)
return response

Django KeyError kwargs.pop('pk')

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.

Add user specific fields to Django REST Framework serializer

I want to add a field to a serializer that contains information specific to the user making the current request (I don't want to create a separate endpoint for this). Here is the way I did it:
The viewset:
class ArticleViewSet(viewsets.ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
filter_class = ArticleFilterSet
def prefetch_likes(self, ids):
self.current_user_likes = dict([(like.article_id, like.pk) for like in Like.objects.filter(user=self.request.user, article_id__in=ids)])
def get_object(self, queryset=None):
article = super(ArticleViewSet, self).get_object(queryset)
self.prefetch_likes([article.pk])
return article
def paginate_queryset(self, queryset, page_size=None):
page = super(ArticleViewSet, self).paginate_queryset(queryset, page_size)
if page is None:
return None
ids = [article.pk for article in page.object_list]
self.prefetch_likes(ids)
return page
The serializer:
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article
def to_native(self, obj):
ret = super(ArticleSerializer, self).to_native(obj)
if obj:
view = self.context['view']
ret['has_liked'] = False
if hasattr(view, 'current_user_liked'):
ret['has_liked'] = obj.pk in view.current_user_liked
return ret
Is there a better place to inject the prefetching of liked articles, or a nicer way to do this in general?
you can do it with SerializerMethodField
Example :
class PostSerializer(serializers.ModelSerializer):
fav = serializers.SerializerMethodField('likedByUser')
def likedByUser(self, obj):
request = self.context.get('request', None)
if request is not None:
try:
liked=Favorite.objects.filter(user=request.user, post=obj.id).count()
return liked == 1
except Favorite.DoesNotExist:
return False
return "error"
class Meta:
model = Post
then you should call serializer from view like this:
class PostView(APIVIEW):
def get(self,request):
serializers = PostSerializer(PostObjects,context={'request':request})
I'd be inclined to try and put as much of this as possible on the Like model object and then bung the rest in a custom serializer field.
In serializer fields you can access the request via the context parameter that they inherit from their parent serializer.
So you might do something like this:
class LikedByUserField(Field):
def to_native(self, article):
request = self.context.get('request', None)
return Like.user_likes_article(request.user, article)
The user_likes_article class method could then encapsulate your prefetching (and caching) logic.
I hope that helps.
According to the Django Documentation - SerializerMethodField, I had to change the code of rapid2share slightly.
class ResourceSerializer(serializers.ModelSerializer):
liked_by_user = serializers.SerializerMethodField()
def get_liked_by_user(self, obj : Resource):
request = self.context.get('request')
return request is not None and obj.likes.filter(user=request.user).exists()

Categories