1
저는 현재 상당히 간단한 장고 프로젝트를 진행 중이며 도움을받을 수 있습니다. 현재 체크 박스를 사용하여 양식을 저장해야합니다. 나는 동료와 함께 저장해야합니다 체크 박스 .Selected profiles_id와 이것에 대한 HTML 양식을 작성 django의 ManyToManyField에 체크 된 값을 저장하는 방법?
class ProfileMatch(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,unique=True)
profiles = models.ManyToManyField(UserProfile,blank=True)
def __unicode__(self):
return "Profile id: %s" %(self.user.id)
이
<form action="." method="POST" class="post-form">{% csrf_token %}
<thead>
<th>Select</th>
<th>Profile-ID</th>
</thead>
<tbody>
{% for user in userprofiles %}
<tr>
<label class="checkbox">
<input type="checkbox" name="user" data-toggle="checkbox" value="{{ user.pk }}">
</label>
<td>{{ user.profile_id }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" class="save btn btn-default">Add Selected </button>
</form>
그리고 간단한을 USER_ID :
나는 ManyToMany와의 관계를 통해 다음과 같은 모델을 가지고 형식을 저장하려면 다음을 클릭하십시오.
def PremuiumUserProfileSelectList(request, pk):
match = ProfileMatch.objects.get(pk=pk)
if request.method == 'POST':
if request.POST.getlist("user", None):
checked = request.POST.getlist("user", None)
premium_user = User.objects.get(pk=pk)
m = match(user=premium_user, profiles=checked)
m.save()
else:
return render(request, "premiumuser/selected_list.html", context)
else:
return render(request, "premiumuser/selected_list.html", context)
저장하지 마십시오. form.Form 필드가 렌더링되고 선택된 모든 profiles_id가 목록에 표시됩니다.이 양식을 어떻게 저장합니까? 선택한 모든 확인란 값을 연관된 사용자에게 저장하려고합니다. 어떻게 저장합니까?
프로필 일치 검색 쿼리가없는 경우 어떻게 일치 개체를 만들 수 있습니까? –
'ProfileMatch.objects.create (user = user)'. manytomany 필드를 추가하면 관련 객체를 추가하기 전에 먼저 객체를 생성해야하기 때문입니다. – itzMEonTV