我有两个模型:“项目”和“发布”。 在发行表中,projectId是与Project表关联的外键。 尝试创建新版本时,输入projectID时,获取:
Cannot assign "'1'": "Release.projectID" must be a "Project" instance
我有三个文件,分别是models.py,forms.py,views.py
models.py:
class Project(models.Model):
JIRAID = models.CharField(max_length=20,null=True)
projectID = models.AutoField(primary_key=True)
projectName = models.CharField(max_length=100)
projectDescription = models.CharField(max_length=100)
projectStartDate = models.DateField()
projectEndDate = models.DateField()
projectEstimatedLOE = models.IntegerField()
createdBy = models.CharField(max_length=30)
createdAt = models.DateTimeField(default=datetime.datetime.now,null=True,blank=True)
updatedAt = models.DateTimeField(default=datetime.datetime.now,null=True,blank=True)
def __str__(self):
return (self.JIRAID, self._get_pk_val, self.projectName, self.projectDescription, self.projectStartDate, self.projectEndDate, self.projectEstimatedLOE,self.createdBy,self.createdAt,self.updatedAt)
class Meta:
db_table='Project'
class Release(models.Model):
JIRAID = models.CharField(max_length=20 )
projectID = models.ForeignKey(Project,on_delete=models.CASCADE,null=True)
releaseID = models.AutoField(primary_key=True)
releaseName = models.CharField(max_length=100)
releaseDescription = models.CharField(max_length=100)
releaseStartDate = models.DateField()
releaseEndDate = models.DateField()
releaseEstimatedLOE = models.IntegerField()
createdBy = models.CharField(max_length=30)
createdAt = models.DateTimeField(default=datetime.datetime.now, null=True, blank=True)
updatedAt = models.DateTimeField(default=datetime.datetime.now, null=True, blank=True)
def __str__(self):
return (self.JIRAID, self.projectID,self._get_pk_val,self.releaseName, self.releaseDescription, self.releaseStartDate, self.releaseEndDate, self.releaseEstimatedLOE,self.createdBy,self.createdAt,self.updatedAt)
class Meta:
db_table='Release'
unique_together = (('projectID', 'releaseID'),)
Views.py:
def releasecreation(request):
context = {'form': Release}
if request.method=='POST':
form = ReleaseCreationForm(request.POST)
if form.is_valid():
JIRAID=request.POST.get('JIRAID')
projectID=Project.objects.get('projectID')
releaseID=request.POST.get('releaseID')
releaseName=request.POST.get('releaseName')
releaseDescription=request.POST.get('releaseDescription')
releaseStartDate=request.POST.get('releaseStartDate')
releaseEndDate=request.POST.get('releaseEndDate')
releaseEstimatedLOE=request.POST.get('releaseEstimatedLOE')
createdBy = User.objects.get(username=request.user.username)
form.save()
return render(request,'releasepages/releasecreateconfirmation.html')
else:
return render(request,'releasepages/releasecreate.html')
else:
return render(request,'releasepages/releasecreate.html',context)
我应该能够通过在创建发行版时在projectID列中下拉菜单来创建发行版。
答案 0 :(得分:0)
尝试一下
projectID=Project.objects.filter(pk=request.POST.get('projectID')).first()
答案 1 :(得分:0)
以下代码与某些地方不太相符:
在上下文中设置表单 class
模板可能需要表单实例而不是类,例如:pd.set_option('display.multi_sparse', False)
(请注意括号)。
还有,这是不是故意与下面使用的{'form': Release()}
不同?
定义了一堆变量,从不使用
除非有一些已删除的代码,否则在ReleaseCreationForm
之后立即定义的所有那些变量都不会使用。
保存后执行http重定向
成功保存表单后,应用程序应发出重定向而不是呈现模板。如果用户决定重新加载页面,这是为了防止重新提交表单。
我建议从此处密切关注视图结构:https://docs.djangoproject.com/en/2.2/topics/forms/#the-view 它具有处理表单所需的所有“条件”,并且它们的结构完全正确。如果想改变这种逻辑,我会非常警告我做错了事。
但是要直接回答您的问题,(手动)创建if form.is_valid():
的实例,您可以执行以下两项操作之一:
Release
或
project_instance = Project.objects.get(projectID=123)
Release.objects.create(
projectID=project_instance,
# other_fields...
)
如您所见,Release.objects.create(
projectID_id=123,
# other_fields...
)
字段会自动在模型上创建附加了ForeignKey
的另一个属性(因此您的_id
和projectID
属性projectID_id
模型)。访问Release
将返回release_instance.projectID
模型的实例,并不其ID。
这可能与预期不符,我猜测将模型字段Project
重命名为projectID
会更干净。