저장 한 후 ManyTomany 관계가있는 필드를 초기 데이터로 MultipleChoiceField에로드하려고합니다.ManytoMany 필드에서 MultipleChoiceField 값으로로드
내 클래스
class Course(models.Model):
name = models.CharField(max_length=30)
description = models.TextField(max_length=30)
owner = models.ForeignKey(User)
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='student')
courses_list = models.ManyToManyField(Course, blank=True)
내 양식
class AddSubscibeForm(forms.ModelForm):
userprofile_set = forms.ModelMultipleChoiceField(initial = User.objects)
class Meta:
model = Course
내보기 사실
def addstudents(request, Course_id):
editedcourse = Course.objects.get(id=Course_id) # (The ID is in URL)
form = AddSubscibeForm(instance=editedcourse)
return render(request, 'addstudents.html', locals())
, 나는 multiplecho이 당신이있는 경우
> editedcourse = Course.objects.get(id=Course_id)
> subscribed = editedcourse.userprofile_set.all()
> subscribed.user.username
: 사용자와 icelist은,하지만 난
가 나는하여 사용자의 cours_list에 액세스 할 수 있습니다 .. 자신의 'courses_list'필드의 과정이 사용자의 목록이없는 아이디어 .. ..
그것은 잘 작동합니다, 공식 문서는 우리가 그렇게하지 않을 것이라고 말하지만 괜찮습니다. 다른 아이디어가 있다면 (의사에게 ^^) https://docs.djangoproject.com/ko/1.4/topics/db/examples/many_to_many/ – nlassaux