title: Django (drf) dynamic permissions from BasePermission
body: <p>I want to have easy way to check if somebody is owner or admin of <code>post</code>, <code>proposal</code> and so on he's trying to edit \ delete.</p>
  
  <p>So, every time I use <code>IsAuthenticated</code> permission and in the method of <code>ModelViewSet</code> I get instance and check if <code>instance.author</code> or sometimes <code>instance.owner</code> is the user who requested it (<code>request.user == instance.owner</code> on some objects it's <code>request.user == instance.author</code>).</p>
  
  <p><strong>Question</strong></p>
  
  <p>The main question is: how can I create permission class that can check this kind of ownership with dynamic user attribute name on instance?</p>
  
  <p><strong>One of mine solutions</strong> (not the best, i think)</p>
  
  <p>I've created function that take user attribute instance name returns permission class:</p>
  
  <pre><code>def is_owner_or_admin_permission_factory(owner_prop_name):
      class IsOwnerOrAdmin(BasePermission):
          def has_permission(self, request, view, *args, **kwargs):
              instance = view.get_object()
              try:
                  owner = getattr(instance, owner_prop_name)
              except AttributeError:
                  return False
              return (
                  request.user and request.user.id and (owner == request.user or request.user.is_staff)
              )
  
      return IsOwnerOrAdmin
  </code></pre>