0
class ProjectTimeAllocation(models.Model):
    id = models.IntegerField(primary_key=True)
    project_id = models.ForeignKey(Projects, models.DO_NOTHING, related_name='pj_id', blank=True, null=True)
    project_manager_id = models.ForeignKey(ProjectManagers, models.DO_NOTHING, blank=True, null=True)
    hours = models.IntegerField(blank=True, null=True)
    week = models.DateTimeField(blank=True, null=True)
    date_created = models.DateTimeField(auto_now=True, null=True)
    last_updated = models.DateTimeField(blank=True, null=True)


    class Meta:
        managed = False
        db_table = 'project_time_allocation'

视图.py

def timeallocation(request, project_manager_id):
    #time_list = ProjectTimeAllocation.objects.order_by('-id')
    if request.method == "POST":
        form = ProjectTimeAllocationForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            #post.project_manager_id = project_manager_id
            post.save()
            messages.success(request, "Updated Time!")
            return HttpResponseRedirect('/dashboard/')
    else:
        form = ProjectTimeAllocationForm()
    return render(request, 'dashboard/timeallocation.html', {'form':form})

表格.py

class ProjectTimeAllocationForm(forms.ModelForm):
    week = forms.DateField(widget=forms.SelectDateWidget())



    class Meta:
        model = ProjectTimeAllocation       
        fields = (
            'hours',
            'week',
            'project_manager_id',
            'project_id',
            )
        widgets = {

        }
        labels = {

        }

我收到一个操作错误(1054,“字段列表中的未知列 'project_id_id'。我不明白额外的 _id 来自哪里?我已尝试清除迁移并进行新的迁移。

4

1 回答 1

0

似乎 django 在 ForeignKeys 上附加了 _id ,因此我不得不从模型和表单中的列名中删除 _id ,然后 django 在生成 sql insert 语句时将其重新附加,现在问题已解决。

于 2018-08-16T21:45:55.427 回答