我在保存包含“直通”类表的m2m数据时遇到了麻烦。我想将所有选定的成员(在表单中选择)保存在通过表中。但是我不知道如何在视图中初始化“通过”表。
我的代码:
class Classroom(models.Model): user = models.ForeignKey(User, related_name = 'classroom_creator') 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", through = 'Membership') class Membership(models.Model): accept = models.BooleanField(User) date = models.DateTimeField(auto_now = True) classroom = models.ForeignKey(Classroom, related_name = 'classroom_membership') member = models.ForeignKey(User, related_name = 'user_membership')
并在视图中:
def save_classroom(request): classroom_instance = Classroom() if request.method == 'POST': form = ClassroomForm(request.POST, request.FILES, user = request.user) if form.is_valid(): new_obj = form.save(commit=False) new_obj.user = request.user new_obj.save() membership = Membership(member = HERE SELECTED ITEMS FROM FORM,classroom=new_obj) membership.save()
我应该如何初始化要正确填充的Membership表的成员资格?
如果使用正常的m2m关系(不通过中间表),则可以替换:
membership = Membership(member = HERE SELECTED ITEMS FROM FORM,classroom=new_obj) membership.save()
与
form.save_m2m()
但是,在使用中间表的情况下,你需要手动处理POST数据并创建具有所有必填字段的Membership对象(类似问题)。最基本的解决方案是将视图更改为:
def save_classroom(request): if request.method == 'POST': form = ClassroomForm(request.POST, request.FILES) if form.is_valid(): new_obj = form.save(commit=False) new_obj.user = request.user new_obj.save() for member_id in request.POST.getlist('members'): membership = Membership.objects.create(member_id = int(member_id), classroom = new_obj) return HttpResponseRedirect('/') else: form = ClassroomForm() return render_to_response('save_classroom.html', locals())
请注意如何操作request.POST(.getlist)。这是因为post和get是具有某些含义的QueryDict对象(request.POST [‘members’]将始终返回一个对象!)。
你可以修改此代码以使其更可靠(错误处理等),并且更详细,例如:
member = get_object_or_404(User, pk = member_id) membership = Membership.objects.create(member = member , classroom = new_obj)
但是请注意,你正在循环中执行一些数据库查询,这通常不是一个好主意(就性能而言)。