Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7033285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:02:48+00:00 2026-05-28T01:02:48+00:00

My application has some hundred users and about 10 to 20 groups. Now the

  • 0

My application has some hundred users and about 10 to 20 groups. Now the customer wants to give special privileges to group leaders.

How would you do this (in a reusable way)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:02:49+00:00Added an answer on May 28, 2026 at 1:02 am
    #------------------------------------------
    # models.py of fooapp
    
    from django.db import models
    from django.contrib.auth.models import User, Group
    
    class GroupLeader(models.Model):
        user=models.OneToOneField(User, primary_key=True)
        groups=models.ManyToManyField(Group, related_name='leaders')
        class Meta:
            verbose_name_plural=u'Group Leaders'
    

    The next snippet makes the group leaders available in django admin:

    #-----------------------------------------
    # admin.py of fooapp
    # See http://stackoverflow.com/questions/2216974/django-modelform-for-many-to-many-fields
    from django.contrib.auth.admin import GroupAdmin
    from fooapp.models import GroupLeader
    
    class MyGroupForm(forms.ModelForm):
        class Meta:
            model=Group
        leaders = forms.ModelMultipleChoiceField(queryset=User.objects.all())
    
        def __init__(self, *args, **kwargs):
            if 'instance' in kwargs:
                # We get the 'initial' keyword argument or initialize it
                # as a dict if it didn't exist.                
                initial = kwargs.setdefault('initial', {})
                # The widget for a ModelMultipleChoiceField expects
                # a list of primary key for the selected data.
                initial['leaders'] = [t.pk for t in kwargs['instance'].leaders.all()]
    
            forms.ModelForm.__init__(self, *args, **kwargs)
            self.fields['leaders'].widget.attrs['size']=10
    
        def save(self, commit=True):
            instance = forms.ModelForm.save(self, False)
    
            # Prepare a 'save_m2m' method for the form,
            old_save_m2m = self.save_m2m
            def save_m2m():
               old_save_m2m()
               instance.leaders.clear()
               for leader_user in self.cleaned_data['leaders']:
                   leader_obj, created = GroupLeader.objects.get_or_create(pk=leader_user.pk) # User object to GroupLeader object
                   instance.leaders.add(leader_obj)
            self.save_m2m = save_m2m
    
            # Do we need to save all changes now?
            if commit:
                instance.save()
                self.save_m2m()
    
    
            return instance
    
    class MyGroupAdmin(GroupAdmin):
        form=MyGroupForm
    admin.site.unregister(Group)
    admin.site.register(Group, MyGroupAdmin)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application has some menu buttons that sends the users to my website. I
My company develops and sells a SaaS application that has hundreds of customers. Some
I believe the application has some parts that target .NET, and some that don't.
Is there a good application (that has some kind of gui) for testing memory
I have a 3yr old application that has some controllers with some very unrestful
I have built a Java application that has some dependencies (~10). I would like
I am working on an application which has got some sensitive information. I am
My iPhone application has several views and some viewControllers for that views. I need
I have a Spring application that I believe has some bottlenecks, so I'd like
I have a web application on ASP.NET (C#) that has some precompiled components. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.