我是Django平台的新手。我有一种情况,我需要在一个页面上显示数据,其中表有多个外键列,如状态,优先级和任务类型。当我保存日期时,它保存外键表中的ID,如StatusTable,PriorityTable和TaskTypeTable 。保存工作正常。但是当我从表中检索日期时,它只提供ID而不是名称。 我怎样才能在Django中实现这一目标?
Modle.py
class StatusTable(models.Model):
status = models.CharField(max_length=20,default='')
def __str__(self):
return self.status
class PriorityTable(models.Model):
priority = models.CharField(max_length=20,default='')
def __str__(self):
return self.priority
class TeamTable(models.Model):
team = models.CharField(max_length=20,default='')
def __str__(self):
return self.team
class TaskTypeTable(models.Model):
tasktype = models.CharField(max_length=30,default='')
def __str__(self):
return self.tasktype
class DatacenterTable(models.Model):
datacenter = models.CharField(max_length=10,default='')
def __str__(self):
return self.datacenter
class TaskMaster(models.Model):
sid = models.CharField(max_length=3)
processor = models.ForeignKey(User,null=True)
tasktype = models.ForeignKey(TaskTypeTable, null=True)
task_title = models.TextField(null=True)
task_description = models.TextField(null=True)
datacenter = models.ForeignKey(DatacenterTable,null=True)
priority = models.OneToOneField(PriorityTable, null=True)
status = models.ForeignKey(StatusTable, null=True)
pid = models.IntegerField(null=True)
sourceincident = models.URLField(null=True)
errorincident = models.URLField(null=True)
processingteam = models.ForeignKey(TeamTable, null=True)
createddate = models.DateField(("Date"), default=datetime.date.today)
duedate = models.DateField(("Date"), default=datetime.date.today)
istaskactive = models.BooleanField(default=True)
forms.py
class CreateTaskMaster(forms.Form):
sid = forms.CharField(required=False,widget=forms.TextInput(attrs={'class': 'form-control mr-sm-2', 'placeholder': 'SID'}))
tasktype_query = TaskTypeTable.objects.values_list('tasktype', flat=True).distinct()
tasktype_query_choices = [('', 'Select TaskType')] + [(region, region) for region in tasktype_query]
tasktype = forms.ChoiceField(tasktype_query_choices, required=True,widget=forms.Select(attrs={'class': 'form-control'}))
task_title = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control mr-sm-2', 'placeholder': 'Task Title', 'rows':4}))
task_description = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control mr-sm-2', 'placeholder': 'Task Description', 'rows':4}))
datacenter_query = DatacenterTable.objects.values_list('datacenter', flat=True).distinct()
datacenter_query_choices = [('', 'Select datacenter')] + [(region, region) for region in datacenter_query]
datacenter = forms.ChoiceField(datacenter_query_choices, required=True,widget=forms.Select(attrs={'class': 'form-control'}))
priority_query = PriorityTable.objects.values_list('priority', flat=True).distinct()
priority_query_choices = [('', 'Select priority')] + [(region, region) for region in priority_query]
priority = forms.ChoiceField(priority_query_choices, required=True,widget=forms.Select(attrs={'class': 'form-control'}))
sourceincident = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control mr-sm-2', 'placeholder': 'Source Incident URL or NO'}))
processingteam_query = TeamTable.objects.values_list('team', flat=True).distinct()
processingteam_query_choices = [('', 'Select processingteam')] + [(region, region) for region in processingteam_query]
processingteam = forms.ChoiceField(processingteam_query_choices, required=True,widget=forms.Select(attrs={'class': 'form-control'}))
duedate = forms.DateField(widget=forms.TextInput(attrs={'class': 'form-control mr-sm-2', 'placeholder': 'Due Date'}))
views.py
form = CreateTaskMaster()
task_title = TaskMaster.objects.filter()
my_list = [ model_to_dict(x) for x in task_title]
print(my_list)
print(my_list[1]['sid'])
return render(request, 'task/task.html', {'form': form, 'sid':my_list})
答案 0 :(得分:0)
无论何时在模型中使用ForeignKey
,都要添加该表的主键以进行记录
class TaskMaster(models.Model):
status = models.ForeignKey(StatusTable, null=True)
class StatusTable(models.Model):
status = models.CharField(max_length=20,default='')
def __str__(self):
return self.status
当新记录添加到TaskMaster
时,状态字段需要StatusTable
的实例,但是当您查看数据库时,状态字段只是整数(StatusTable
的主键)
如果直接转换TaskMaster
做词典,你将获得主键(一个数字),这就是你需要序列化器的原因,但是知道使用这个代码。
my_list = [ model_to_dict(x)['status'] = x.status.status for x in task_title]