为什么django从模型中删除字段?

时间:2017-01-17 22:40:28

标签: python django forms

我有一个跟踪模型,并且有一个外国的证据模型。我试图上传与该跟踪相关的证据文件...但是当我尝试保存时我得到了这个错误:

Request Method:     POST
Request URL:    http://127.0.0.1:8000/myapp/change_actions/tracking/create/1/
Django Version:     1.10.1
Exception Type:     RelatedObjectDoesNotExist
Exception Value:    

Evidence has no tracking.

我的视图保存了跟踪表单,然后将该记录的ID传递给证据表单作为外来.....但该错误就像表单正在消除跟踪字段!! ....这些是我的文件:

Models.py

class Tracking(ModelBase):    
    activity = models.ForeignKey(
        Activity,
        verbose_name = 'Actividad', null = False)
    code = models.CharField('Codigo de seguimiento', max_length=50)
    observation = models.TextField('Avance a la fecha', max_length=2000)
    tracking_date = models.DateTimeField('Fecha de seguimiento')


    def __str__(self):
        return self.observation

class Evidence(ModelBase):

    tracking = models.ForeignKey(
        Tracking,
        related_name = 'Tracking_evidence',
        verbose_name = 'Seguimiento', null = False)
    evidence = models.FileField(verbose_name = 'Evidencia', null = True, upload_to='documents/')

这是form.py:

class EvidenceForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        self.tracking = kwargs.pop('tracking')
        super(EvidenceForm, self).__init__(*args, **kwargs)

    def clean(self, *args, **kwargs):
        cleaned_data = super(EvidenceForm, self).clean()
        self.instance.tracking.id = self.tracking

    class Meta:
        model = Evidence
        fields = ['tracking', 'evidence']

以及对它的看法:

class TrackingCreateView(View):

    template_name = ['change_actions/tracking_create.html']
    breadcrumb = [{'name': 'Registro de seguimiento', 'url': None}]

    @method_decorator(login_required)
    def get(self, request, *args, **kwargs):
        form = TrackingForm()
        form_evidence = EvidenceForm(tracking = None)
        activity = Activity.objects.get(pk = kwargs['activity_id'])
        return render(request, self.template_name,{
            'form_tracking': form,'breadcrumb': self.breadcrumb,
            'activity': activity,'form_evidence': form_evidence})

    def post(self, request, *args, **kwargs):
        form = TrackingForm(request.POST)
        activity = Activity.objects.get(pk = request.POST['activity_id'])
        self.breadcrumb[0]['url'] = reverse(
            'change_actions:tracking_create',
            kwargs = {'activity_id': activity.id })
        context = {
            'breadcrumb': self.breadcrumb, 'activity': activity,
            'form_tracking': form}

        if form.is_valid():
            form.save()
            form_evidence = EvidenceForm(
                request.POST, request.FILES, tracking = form.instance.id)

            if form_evidence.is_valid():
                form_evidence.save()
                messages.success(request, 'Seguimiento registrado correctamente')
                return HttpResponseRedirect(reverse(
                    'change_actions:activity_detail',
                    kwargs = {'activity_id': activity.id }))
            else:
                context.update({'form_evidence': form_evidence}) 
                return render(request, self.template_name, context)
        else:
            context['form_tracking'] = form
            return render(request, self.template_name, context)

知道我为什么会收到这个错误吗?提前谢谢!

0 个答案:

没有答案