python Django -如何在获取values_list时过滤?

kiz8lqtg  于 2022-11-28  发布在  Python
关注(0)|答案(1)|浏览(103)

我有一个应用程序,其中有多个对象与一个模型相关。当我尝试在窗体中显示数据(以便更新)时,它要么给出错误,要么不显示任何数据。
为了说明布局,我们使用OBJECT(ID)

Project(1):
    Issue(1)
    Issue(42)
    Issue(66)
    Issue(97)

上面发生的事情是我有多个与项目相关的问题。我通过使用下面的查询集从项目中的问题中获取ID。

get_issue_id = get_object_or_404(DevProjects, pk=pk)
issue_id = DevIssues.objects.filter(project=get_issue_id).values_list('id', flat=True)

返回:

<QuerySet [1, 42, 66, 97]>

我尝试使用以下查询集从values_list中筛选问题ID,以便将Instance=(对于表单)设置为查询集,以便仅获取数据并在表单中显示我请求的项目PK中问题ID的数据。

update_issue = DevIssues.objects.filter(id=issue_id)

下面是我目前的view.py

get_issue_id = get_object_or_404(DevProjects, pk=pk)
issue_id = DevIssues.objects.filter(project=get_issue_id).values_list('id', flat=True)
update_issue = DevIssues.objects.filter(id=issue_id)
update_issue_form = UpdateProjectIssues(instance=update_issue)
if request.method == 'POST' and 'updateissue' in request.POST:
      update_issue_form = UpdateProjectIssues(request.POST, instance=update_issue)
            if update_issue_form.is_valid():
                update_issue_form.save()

下面是models.py开发项目和开发问题的www.example.com:

class DevProjects(models.Model):
    PROJECT_TYPE = [
        ('NEW_APP', 'New Application'),
        ('UPDATE_APP', 'Update Application'),
        ('BUG_FIX', 'Bug Fixes')
    ]
    PROJECT_STATUS = [
        ('New', 'New'),
        ('In Progress', 'In Progress'),
        ('Complete', 'Complete'),
    ]
    project_id = models.CharField(max_length=15, editable=False)
    project_title = models.CharField(max_length=100)
    project_desc = models.CharField(max_length=500)
    project_category = models.CharField(max_length=25, choices=PROJECT_TYPE, null=True, blank=True)
    project_status = models.CharField(max_length=25, choices=PROJECT_STATUS, default='New')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateField(auto_now=True)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)

    def save(self, *args, **kwargs):
        super(DevProjects, self).save(**kwargs)
        self.project_id = 'PROJ-' + str(self.id)
        super(DevProjects, self).save(**kwargs)

    def __str__(self):
        return self.project_title

class DevIssues(models.Model):
    ISSUE_CODE = [
        ('BUG', 'Bug'),
        ('BACKLOG', 'Backlog'),
        ('REQUEST', 'Request'),
        ('TODO', 'To-Do'),
    ]
    ISSUE_STATUS = [
        ('New', 'New'),
        ('In Progress', 'In Progress'),
        ('Complete', 'Complete'),
    ]
    issue_id = models.CharField(max_length=15, editable=False)
    project = models.ForeignKey(DevProjects, on_delete=models.CASCADE, related_name='issue')
    issue = models.CharField(max_length=100)
    issue_desc = models.CharField(max_length=500)
    issue_code = models.CharField(max_length=9, choices=ISSUE_CODE, null=True, blank=True)
    issue_status = models.CharField(max_length=15, choices=ISSUE_STATUS, default='New')
    issue_resolution = models.CharField(max_length=500, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateField(auto_now=True)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)

    def save(self, *args, **kwargs):
        self.issue_id = 'ISSUE-' + str(self.id)
        super(DevIssues, self).save(**kwargs)

这里是我的forms.py:

class UpdateProjectIssues(forms.ModelForm):
    class Meta:
        model = DevIssues
        fields= ["issue", "issue_desc", "issue_code", "issue_status"]
        labels = {
            'issue_status': 'Update Status'
        }

而这就是我目前面临的错误:

'QuerySet' object has no attribute '_meta'

当我使用get()时,得到的错误如下:

The QuerySet value for an exact lookup must be limited to one result using slicing.

当我使用id__in这样的代码时,我会遇到另一个错误:

get() returned more than one DevIssues -- it returned 2!

如何过滤'issue_id'(values_list查询集)以获取数据并在表单中显示正确的数据?

afdcj2ne

afdcj2ne1#

获取项目示例

project_id = get_object_or_404(DevProjects, pk=pk)

获取与该项目例程相关的issue_id

issue_ids = DevIssues.objects.filter(project=project_id).values_list('id', flat=True)

获取更新问题对象

update_issue = DevIssues.objects.filter(id__in=issue_ids)

但在这一行中已经得到了相同的结果

update_issue = DevIssues.objects.filter(project=project_id)

所以,不需要输入查询。
出现错误,因为在此行中传递的是queryset而不是object

update_issue_form = UpdateProjectIssues(instance=update_issue)

您可以迭代查询集并在ModelForm类中传递对象。

相关问题