Created
June 4, 2010 09:06
-
-
Save svetlyak40wt/425182 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
# 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