使用formsets中的formset

时间:2013-06-19 18:03:27

标签: python html django forms

我在使用Django编写的网站上的挑战注册表中尝试添加更多复杂功能时遇到了困难。所以,这是交易:

当用户创建新的挑战时,他们必须计划会议项目。我想以一种必须在同一页面上定义所有内容的方式构建表单。

以下是我的模特:

class Challenge(models.Model):
    description = models.TextField()
    name = models.CharField(verbose_name=_("What is the challenge?"), max_length=60)
    url = models.URLField(verbose_name=_("URL"), blank=True)
    solutions = models.TextField(verbose_name=_("What are the justifications for the challenge?"), blank=True)
    prize_description = models.TextField(verbose_name=_("What are the prizes for the participants?"), blank=True)

    subjects = models.ManyToManyField(Subject, verbose_name=_("What kind of subjects participants can learn?"), blank=True)
    skills = models.ManyToManyField(Skill, verbose_name=_("What skills and abilities do you want to promote?"), blank=True)

    def __unicode__(self):
        return self.name

class Meeting(models.Model):
    start_date = models.DateField(verbose_name=_("Start Date"))
    end_date = models.DateField(verbose_name=_("End Date"))
    start_time = models.TimeField(verbose_name=_("Start Time"))
    end_time = models.TimeField(verbose_name=_("End Time"))
    summary = models.CharField(max_length=60)
    description = models.TextField()
    challenge = models.ForeignKey(Challenge)

    def __unicode__(self):
        return self.summary;

class Project(models.Model):
    start_time = models.TimeField(verbose_name=_("Start Time"))
    end_time = models.TimeField(verbose_name=_("End Time"))
    description = models.CharField(max_length=255)
    meeting = models.ForeignKey(Meeting)

    def __unicode__(self):
        return self.description;

这是我的表格:

@login_required(login_url='/accounts/login/')
def new_challenge(request, template_name='challenges/new.html'):
    MeetingInlineFormSet = inlineformset_factory(Challenge, Meeting, form=MeetingForm, extra=1)
    ProjectInlineFormSet = inlineformset_factory(Meeting, Project, form=ProjectForm)

    if request.method == 'POST':
        challenge_form = ChallengeForm(request.POST)
        meeting_inline_formset = MeetingInlineFormSet(request.POST, instance=Challenge())
        project_inline_formset = ProjectInlineFormSet(request.POST, instance=Meeting())

        if challenge_form.is_valid():
            challenge = challenge_form.save()

            if meeting_inline_formset.is_valid():
                for meeting_form in meeting_inline_formset:             
                    meeting = meeting_form.save(commit=False)                           
                    meeting.challenge = challenge
                    meeting.save()

            return HttpResponseRedirect(reverse('challenges.views.challenge_detail', args=(challenge.pk,)))
    else:
        challenge_form = ChallengeForm()
        meeting_inline_formset = MeetingInlineFormSet(instance=Challenge())
        project_inline_formset = ProjectInlineFormSet(instance=Meeting())

    c = {'challenge_form': challenge_form,
         'meeting_inline_formset' : meeting_inline_formset,                                 
         'project_inline_formset': project_inline_formset }

    return render_to_response(template_name, c, context_instance=RequestContext(request))

问题是我无法弄清楚我必须做些什么来保存会议中的项目。挑战有很多会议,在每次会议中,我都有一个包含项目表格的表格集,但我找不到告诉哪些项目属于哪些会议的方式。

有没有办法解决这个问题?我考虑使用一些隐藏的字段从UI获取此信息。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

谷歌搜索“formset中的django formset”产生了这个链接:

Nested Formsets with Django