Skip to content

Instantly share code, notes, and snippets.

@svetlyak40wt
Created June 4, 2010 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save svetlyak40wt/425182 to your computer and use it in GitHub Desktop.
Save svetlyak40wt/425182 to your computer and use it in GitHub Desktop.
# ...
# There are models Subscription and MailList defined above.
# MailList has a many to many field 'managers'. This field
# defines which manager is able to edit/add subscriptions to
# a maillist.
#
# To filter choices in the Django's admin, I use following code:
class SubscriptionAdmin(admin.ModelAdmin):
list_display = ('id', 'mail_list', 'email', 'name')
def get_form(self, request, obj=None, **kwargs):
form_class = super(SubscriptionAdmin, self).get_form(request, obj = obj, **kwargs)
class ModelFormWithPermissions(form_class):
def __init__(self, *args, **kwargs):
super(ModelFormWithPermissions, self).__init__(*args, **kwargs)
self.fields['mail_list'].queryset = \
MailList.objects.filter(
managers__id = request.user.id
)
return ModelFormWithPermissions
site.register(Subscription, SubscriptionAdmin)
# This code creates a special ModelForm class which know
# about the current user, his 'id' and permissions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment