Django ManyToMany Membership errors making associations

Posted by jmitchel3 on Stack Overflow See other posts from Stack Overflow or by jmitchel3
Published on 2012-09-19T21:36:07Z Indexed on 2012/09/19 21:37 UTC
Read the original article Hit count: 230

Filed under:
|

I'm trying to have a "member admin" in which they have hundreds of members in the group. These members can be in several groups. Admins can remove access for the member ideally in the view. I'm having trouble just creating the group. I used a ManytoManyField to get started. Ideally, the "member admin" would be able to either select existing Users OR it would be able to Add/Invite new ones via email address.

Here's what I have:

#views.py
def membership(request):
    group = Group.objects.all().filter(user=request.user)
    GroupFormSet = modelformset_factory(Group, form=MembershipForm)

if request.method == 'POST':
    formset = GroupFormSet(request.POST, request.FILES, queryset=group)
    if formset.is_valid():
        formset.save(commit=False)
        for form in formset:
            form.instance.user = request.user
        formset.save()
        return render_to_response('formset.html', locals(), context_instance=RequestContext(request))
else:
    formset= GroupFormSet(queryset=group)
return render_to_response('formset.html', locals(), context_instance=RequestContext(request))

 #models.py 
class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(User, related_name='community_members', through='Membership')
    user = models.ForeignKey(User, related_name='community_creator', null=True)

    def __unicode__(self):
        return self.name

class Membership(models.Model):
    member = models.ForeignKey(User, related_name='user_membership', blank=True, null=True)
    group = models.ForeignKey(Group, related_name='community_membership', blank=True, null=True)
    date_joined = models.DateField(auto_now=True, blank=True, null=True)

    class Meta:
       unique_together = ('member', 'group')

Any ideas? Thank you for your help.

© Stack Overflow or respective owner

Related posts about django

Related posts about django-many-to-many