Django m2m form appearing fields
- by dana
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_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?
Thanks!