开发者

Django m2m form appearing fields

开发者 https://www.devze.com 2023-01-03 00:09 出处:网络
I have a classroom application,and a follow relation. Users can follow each other and can create classrooms.When a user creates a classroom, he can invite only the people that are following him.

I have a classroom application,and a follow relation. Users can follow each other and can create classrooms.When a user creates a classroom, he can invite only the people that are following him. The Classroom model is a m2m to User table.

i have in models. py:

class Classroom(models.Model):
     creator = models.ForeignKey(User)
     classname = models.CharField(max_length=140, unique = True)
     date = models.DateTimeField(auto_now=True)
     open_class = models.BooleanField(default=True)
     members = models.ManyToManyField(User,related_name="list of invited members")

and in models.py of the follow application:

class Relations(models.Model):    
    initiated_by = models.ForeignKey(User, editable=False)
    date_initiated = models.DateTimeField(auto_now=True, editable = False)
    follow = models.ForeignKey(User, editable = False, related_name = "follow") 
    date_follow = models.DateTimeField(auto_now=True, editable = False)

and in views.py of the classroom app:

def save_classroom(request, username):

   if request.method == 'POST':
        u = User.objects.get(username=username)
        form = ClassroomForm(request.POST, request.FILES) 
        if form.is_valid():
           new_obj = form.save(commit=False)
           new_obj.creator = request.user 
           r = Relations.objects.filter(initiated_by = request.user)
         #  new_obj.members = 
           new开发者_StackOverflow社区_obj.save()
           return HttpResponseRedirect('.')    
   else:
           form = ClassroomForm()     
   return render_to_response('classroom/classroom_form.html', {
           'form': form,

           }, 
          context_instance=RequestContext(request))  

i'm using a ModelForm for the classroom form, and the default view, taking in consideration my many to many relation with User table, in the field Members, is a list of all Users in my database. But i only want in that list the users that are in a follow relationship with the logged in user - the one who creates the classroom. How can i do that?

p.s: also, when i'm saving the form, it saves, but without "members"

Thanks!


You have to change the queryset that is used to populate the formfield... Put the following in your form class:

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user')
        super(ClassroomForm, self).__init__(*args, **kwargs)
        relations = Relations.objects.filter(initiated_by=user)
        self.fields["members"].queryset = \
            User.objects.filter(pk__in=[r.follow.pk for r in relations])

To get the current user into the form's __init__ method, change its initsialisation in your save_classrom view:

        form = ClassroomForm(request.POST, request.FILES, user=request.user) 
        # and after the else:
        form = ClassroomForm(user=request.user)     

I'm not quite sure about the query to get the users for your field, but i think it should be checked that initiated_by is the logged in user?

To save the m2m relations you also have to call form.save_m2m()!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号