从ManytoMany字段中加载MultipleChoiceField值 [英] Load in a MultipleChoiceField values from a ManytoMany field

查看:151
本文介绍了从ManytoMany字段中加载MultipleChoiceField值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的课程 p>

 课程课程(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 =真的)
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(ini​​tial = User.objects)
class Meta:
model =课程

我的视图

  def addstudents(request,Course_id):
editedcourse = Course.objects.get(id = Course_id)#(ID在URL中)
form = AddSubscibeForm(instance = editedcourse)
return render(request,'addstudents.html'本地人())

其实我有一个多用户的用户,但是我没有列表的用户在'courses_list'字段中有课程。



我可以通过a访问用户的cours_list:

 > editedcourse = Course.objects.get(id = Course_id)
> subscribed = editedcourse.userprofile_set.all()
> subscribed.user.username

如果你有一个想法..:)

解决方案

确认你在问什么您希望能够看到一个带有课程的表单,并选择哪些用户在课程中拥有该课程?



您将无法正确使用ModelForm在其模型中不存在。



您可以做的是更改模型,并有一个ManyToManyField指向两种方式,然后使用以下内容:

  class AddSubscibeForm(forms.ModelForm):
class Meta:
model =课程
fields =('userProfiles ')

假设你有一个ManyToManyField在课程调用 userProfiles



要让ManyToManyField工作两种方法,请查看这张票



我还没试过这个,但我认为它应该工作。

  class Test1(models.Model):
tests2 = models.ManyToManyField 'Test2',blank = True)

class Test2(models.Model):
tests1 = models.ManyToManyField(Test1,through = Test1.tests2.through,blank = True)

或这个:

  class User .Model):
groups = models.ManyToManyField('Group',through ='UserGroups')

class Group(models.Model):
users = models.ManyToManyField 'User',through ='UserGroups'

class UserGroups(models.Model):
user = models.ForeignKey(User)
group = models.ForeignKey(Group)

class Meta:
db_table ='app_user_groups'
auto_created =用户

上述两者都可以正常工作。而在这两种情况下,您都不必更改数据库中的任何内容。


I want to load in a MultipleChoiceField as initial data a field with a manytomany relation, after save it.

My classes

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)

My form

class AddSubscibeForm(forms.ModelForm):
    userprofile_set = forms.ModelMultipleChoiceField(initial = User.objects)
    class Meta:
        model = Course

My view

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())

Actually, I have a multiplechoicelist with users, but i don't have the list of users that have the course in their 'courses_list' field..

I can access to the user's cours_list by a :

> editedcourse = Course.objects.get(id=Course_id)
> subscribed = editedcourse.userprofile_set.all()
> subscribed.user.username

If you have an idea.. :)

解决方案

To confirm what you're asking. You want to be able to see a form with a Course and choose which Users have that course in their courses?

You will NOT be able to properly use a field in a ModelForm that does not exist in its Model.

What you can do is change the model and have a ManyToManyField pointing both ways and then use the following:

class AddSubscibeForm(forms.ModelForm):
    class Meta:
        model = Course
        fields = ('userProfiles')

This would work assuming you have a ManyToManyField in Courses called userProfiles.

To get the ManyToManyField to work both ways, take a look at this ticket.

I haven't tried this but I think it should work.

class Test1(models.Model):
    tests2 = models.ManyToManyField('Test2', blank=True)

class Test2(models.Model):
    tests1 = models.ManyToManyField(Test1, through=Test1.tests2.through, blank=True)

or this:

class User(models.Model):
    groups = models.ManyToManyField('Group', through='UserGroups')

class Group(models.Model):
    users = models.ManyToManyField('User', through='UserGroups')

class UserGroups(models.Model):
    user = models.ForeignKey(User)
    group = models.ForeignKey(Group)

    class Meta:
        db_table = 'app_user_groups'
        auto_created = User

Both of the above should work. And in both cases you shouldn't have to change anything in the database.

这篇关于从ManytoMany字段中加载MultipleChoiceField值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆