我有2个模特
class Task(models.Model):
taskid = models.AutoField(primary_key=True,default = increment_booking_number)
projectcode = models.ForeignKey('Project', models.DO_NOTHING, db_column='projectid')
taskname = models.CharField(max_length=100)
taskdescription = models.TextField(max_length=500, blank=True, null=True)
和
class Project(models.Model):
projectid = models.AutoField(primary_key=True,default = increment_project_number)
teamcode = models.ForeignKey('Teammember', models.DO_NOTHING, db_column='teamcode', blank=True, null=True)
projectname = models.CharField(max_length=40, blank=True, null=True)
projectdescription = models.CharField(max_length=255, blank=True, null=True)
在我的表单中我想显示项目名称,但它显示“对象对象”
class TaskForm(forms.ModelForm):
class Meta:
model = Task
fields = (
'projectcode',
'taskname',
'taskdescription',)
我试过这样的事:projectname = forms.ModelChoiceField(queryset = Project.objects.all())但是它不好......
请问任何建议?
答案 0 :(得分:0)
class Project(models.Model):
projectid = models.AutoField(primary_key=True,default = increment_project_number)
teamcode = models.ForeignKey('Teammember', models.DO_NOTHING, db_column='teamcode', blank=True, null=True)
projectname = models.CharField(max_length=40, blank=True, null=True)
projectdescription = models.CharField(max_length=255, blank=True, null=True)
def __str__(self):
return self.projectname
将 str 方法添加到您的模型中会很好。更多信息here
答案 1 :(得分:0)
好吧,你必须在任务模型
的末尾添加str()方法 class Task(models.Model):
taskid = models.AutoField(primary_key=True,default = increment_booking_number)
projectcode = models.ForeignKey('Project', models.DO_NOTHING, db_column='projectid')
taskname = models.CharField(max_length=100)
taskdescription = models.TextField(max_length=500, blank=True, null=True)
#add this
def __str__(self):
return self.projectname
str()方法允许我们返回一个漂亮的,人类可读的模型表示以获取更多信息,请参阅str()方法的文档
注意:强> 您必须在任务模型类上面添加此代码,以防您使用Python2:
@python_2_unicode_compatible # only if you need to support Python 2
答案 2 :(得分:0)
{{1}}