daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. django admin filter change_list fields according to the user group
  2. class Product(models.Model):
  3.     name = models.CharField(...)
  4.     description = models.CharField(...)
  5.     approved = models.CharField(max_length=1, choices=(('y', 'yes'), ('n','no'), ('w', 'waiting'))
  6.    
  7. def get_form(self, request, obj=None, **kwargs):
  8.     ....
  9.     ....
  10.     defaults = {
  11.         "form": self.form,
  12.         "fields": fields,
  13.         "exclude": exclude,
  14.         "formfield_callback": curry(self.formfield_for_dbfield, request=request),
  15.     }
  16.     defaults.update(kwargs)
  17.     ...
  18.    
  19. def __init__(self,*args, **kwargs):      
  20.         self.user=None
  21.         if kwargs:
  22.             self.user = kwargs.pop('user')
  23.    
  24. def is_user_in_group(user,group_name):
  25.     return user.groups.filter(name=group_name).count() == 1
  26.    
  27. self.fields['some_field_to_hide'].widget = widgets.HiddenInput
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top