[Fixed]-How to send additional attribute to assigned method?

1👍

You can’t without making changes to UserPassesTestMixin. An easier solution may be to supply a kwarg to the view in the urls.py file or create a new subclass of the View with a different profile_type property on the class.

For example:

class CompanyProfileUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = CompanyProfile
    template_name = 'profiles/create.html'
    fields = ['user', 'name']

    def test_func(self):
        if self.kwargs['profile_type'] == 'user':
            if self.request.user.profile_url == self.kwargs['profile_url']:
                return True
            else:
                raise PermissionDenied
        else:
            # something
            pass

urlpatterns += [
    url('^$', views.CompanyProfileUpdateView.as_view(), name='update_user', kwargs={'profile_type': 'user'}),
    url('^$', views.CompanyProfileUpdateView.as_view(), name='update_other', kwargs={'profile_type': 'other type'})
]

Second option:

class CompanyProfileUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = CompanyProfile
    template_name = 'profiles/create.html'
    fields = ['user', 'name']

    profile_type = 'user'
    def test_func(self):
        if self.profile_type == 'user':
            if self.request.user.profile_url == self.kwargs['profile_url']:
                return True
            else:
                raise PermissionDenied
        else:
            # something
            pass

class OtherProfileUpdateView(CompanyProfileUpdateView):
    profile_type = 'other type'

Leave a comment