关于表单和实例

时间:2017-10-25 07:51:23

标签: django

有时,我必须显示多个复选框以检查或取消选中某些项目。因此表单可能已经选中了一些复选框。为此,我使用$action = $_POST['action']; $variantOpt = $_POST['form'][0]['variantOpt']; $fName = $_POST['form'][0]['fName']; $lName = $_POST['form'][0]['lName']; $address = $_POST['form'][0]['address']; $doBirth = $_POST['form'][0]['doBirth']; $city = $_POST['form'][0]['city']; $zipCode = $_POST['form'][0]['zipCode']; $email = $_POST['form'][0]['email']; $phone = $_POST['form'][0]['phone']; $remarks = $_POST['form'][0]['remarks'];

这里的问题,我无法做到,因为我的多对多字段都有一个form = myform(instance=manyTomanyField)参数:

through

确实,我正在使用games = models.ManyToManyField(Games, through="Relation" verbose_name="Jeu")

我没有错误消息,但没有选中复选框。通常我应该默认选中所有。

views.py:

through="Relation"

Models.py:

def view_team_recruitment(request):
    if request.user.is_authenticated():
        media = settings.MEDIA

        form = TeamRecruitmentForm(instance=Team.objects.get(owner=request.user))

forms.py

class Team(models.Model):
    name = models.CharField(max_length=15, null=False)
    tag = models.CharField(max_length=4, null=False)
    description = HTMLField(blank=True, null=True, default='')
    logo = models.FileField(upload_to=user_directory_path, validators=[validate_file_extension], blank=True, null=True)
    games = models.ManyToManyField(Games, through="Relation", verbose_name="Jeu")
    owner = models.ForeignKey(User, verbose_name="Créateur", related_name='myteam')
    date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de création")
    update = models.DateTimeField(auto_now=True, verbose_name="Dernière modification")

    def __str__(self):
        return self.name

class Relation(models.Model):
    on_team = models.ForeignKey(Team, verbose_name="Equipe")
    on_game = models.ForeignKey(Games, verbose_name="Jeu")
    on_plateform = models.ForeignKey(Plateform, verbose_name="Plateforme")
    recruitment = models.BooleanField(default=True)

class Plateform(models.Model):
    name = models.CharField(max_length=100, unique=True, null=False, verbose_name="Plateforme")
    guid = models.CharField(max_length=100, unique=True, null=False, verbose_name="Abréviation")
    date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de création")
    update = models.DateTimeField(auto_now=True, verbose_name="Dernière modification")
    def __str__(self):
        return self.name

class Games(models.Model):
    guid = models.CharField(max_length=100, unique=True, null=False, verbose_name="GUID")
    title = models.CharField(max_length=100, null=False, verbose_name="Titre")
    logo = models.FileField(upload_to='media/games/', validators=[validate_file_extension], blank=True, null=True, verbose_name="Logo du jeu")
    date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de création")
    update = models.DateTimeField(auto_now=True, verbose_name="Dernière modification")
    plateform = models.ManyToManyField(Plateform, verbose_name="Plateforme")
    def __str__(self):
        return self.title

我想要的很简单,目前,渲染是: enter image description here

我想根据我的模型检查任何方框。一般来说,我的方法适用于简单的“多对多”字段......

谢谢大家

1 个答案:

答案 0 :(得分:0)

尝试使用此查询:

qv = Team.objects.get(owner=request.user)
instance = Relation.objects.filter(on_team=qv.id)

def view_team_recruitment(request):
    if request.user.is_authenticated():
        media = settings.MEDIA

        form = TeamRecruitmentForm(instance=Team.objects.get(owner=request.user))

您的表单适用于模型:关系和您的实例查询是:团队

form = TeamRecruitmentForm(instance=Team.objects.get(owner=request.user))