基于类的通用UpdateView内联

时间:2012-01-30 20:44:58

标签: django django-forms django-views

我有以下型号

class Cv(models.Model):
    name = models.CharField(_('name'), max_length=250)
    objective = models.CharField(_('objective'), max_length=250)
    slug = models.SlugField(editable=False)

class Position(models.Model):
    cv = models.ForeignKey(Cv, verbose_name=_('cv'))
    start = models.DateField(_('start'))
    end = models.DateField(_('end'))
    name = models.CharField(_('name'), max_length=250)
    currently_employed = models.BooleanField(_('currently employed'))
    sector = models.IntegerField(_('sector'), choices=SECTOR_CHOICES)
    duties = models.TextField(_('duties'))

以下表格:

class CvForm(ModelForm):
    class Meta:
        model = Cv

class PositionForm(ModelForm):
    class Meta:
        model = Position
        widgets = {
            'start': DateInput(attrs={'class':'datepicker'}),
            'end': DateInput(attrs={'class':'datepicker'}),
        }

PositionFormSet = inlineformset_factory(Cv, Position, form=PositionForm, extra=1)

我创建了以下通用CreateView,效果很好:

class CreateCvView(CreateView):
    model = Cv
    form_class = CvForm
    template_name = 'recruitment/cv/cv_detail.html'

    def get_success_url(self):
        self.success_url = '/'
        return self.success_url

    def get_context_data(self, **kwargs):
        context = super(CreateCvView, self).get_context_data(**kwargs)
        if self.request.POST:
            context['position_form'] = PositionFormSet(self.request.POST)
        else:
            context['position_form'] = PositionFormSet(instance=self.object)
        return context

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.slug = slugify(self.request.user)
        context = self.get_context_data()
        position_form = context['position_form']
        if position_form.is_valid():
            self.object = form.save()
            position_form.instance = self.object
            position_form.save()
            return HttpResponseRedirect(self.get_success_url())
        else:
            return self.render_to_response(self.get_context_data(form=form))

我想要做的是UpdateView,所以我复制了createview并将其传递给了slug,如下所示:

class EditCvView(UpdateView):
    model = Cv
    form_class = CvForm
    template_name = 'recruitment/cv/cv_detail.html'

    def get_success_url(self):
        self.success_url = '/'
        return self.success_url

    def get_context_data(self, **kwargs):
        context = super(EditCvView, self).get_context_data(**kwargs)
        if self.request.POST:
            context['position_form'] = PositionFormSet(self.request.POST)
        else:
            context['position_form'] = PositionFormSet(instance=self.object)
        return context

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.slug = slugify(self.request.user)
        context = self.get_context_data()
        position_form = context['position_form']
        if position_form.is_valid():
            self.object = form.save()
            position_form.instance = self.object
            position_form.save()
            return HttpResponseRedirect(self.get_success_url())
        else:
            return self.render_to_response(self.get_context_data(form=form))

它加载正常,但是当我发布表单时,我在get_context_data中的PositionFormSet(self.request.POST)上得到了“列表索引超出范围”错误。还有其他人有这个问题吗?

1 个答案:

答案 0 :(得分:0)

在EditCvView中:

context [' position_form'] = PositionFormSet(self.request.POST,instance = self.object)