如何以这种方式过滤数据?

时间:2022-01-20 21:14:06

models.py:

class InjuredLocation(models.Model):
    reportperson = models.ForeignKey(ReportPerson)
    mark1 = models.BooleanField('Mark1', default=False)
    mark2 = models.BooleanField('Mark2', default=False)
    mark3 = models.BooleanField('Mark3', default=False)       

class Report(models.Model):
   user = models.ForeignKey(User, null=False)
   report_number = models.CharField('report Number', max_length=100)

class ReportPerson(models.Model):   
    report = models.ForeignKey(Report)
    action_type = models.CharField(max_length=100, choices=ACTION_TYPE)
    name = models.CharField('Name', max_length=100)

This is my three models, I want to filter the data from InjuredLocation models.

这是我的三个模型,我想从InjuredLocation模型中过滤数据。

  1. Reportperson table contain report id and name field of that table could be multiple.Each report can have multiple names.
  2. Reportperson表包含该表的报告ID和名称字段可以是多个。每个报告可以有多个名称。

  3. I want to filter the data from InjuredLocation table with reference to reportperson_id.
  4. 我想参考reportperson_id过滤InjuredLocation表中的数据。

  5. The filtered data should be for the equivalent report.
  6. 过滤后的数据应该是等效报告。

tried:

 injury_list = []
    reportperson = ReportPerson.objects.filter(report=report_id, action_type="involved")    
    injuary_mark = InjuredLocation.objects.filter(pk=reportperson)
    for injuary in injuary_mark:
        mark = InjuredLocation.objects.get(pk=injuary.id)
        marklist={'mark':mark}        
        injury_list.append(marklist) 

I am getting this error "(1242, 'Subquery returns more than 1 row')" in 5th line,if Reportperson table have more than one name.

我收到此错误“(1242,'子查询返回超过1行')”在第5行,如果Reportperson表有多个名称。

update:

injuery_list = []
injuries = InjuredLocation.objects.filter(reportperson__report=report_id, reportperson__action_type="involved")
for reportperson in injuries:    
    injun = InjuredLocation.objects.get(pk=reportperson.id)
    list_inju = {'person': injun}
    injuery_list.append(list_inju)

Able to take the objects from InjuredLocation models,in template i rendered it but problem is "it should render against reportperson_id,instead it is rendering all" for example if InjuredLocation models have reportperson_id=1,mark1=0 & mark2=1 and for reportperson_id=2,mark1=1 & mark2=0 it is rendering all like this "1 1" for both reportperson_id.The expected output is 0 1 and 1 0.What ever selected are all comes to display for all reportperson_id.

能够从InjuredLocation模型中获取对象,在模板中我呈现它,但问题是“它应该呈现为reportperson_id,而是呈现所有”,例如,如果InjuredLocation模型具有reportperson_id = 1,mark1 = 0&mark2 = 1并且对于reportperson_id = 2,mark1 = 1&mark2 = 0这两个reportperson_id都呈现这样的“1 1”。预期的输出是0 1和1 0.所有选择的都是为所有reportperson_id显示的。

template is

{%for injuary_mark in injuery_list%}
    {%if injuary_mark.person.mark1 %}<img style="float: right; margin:5px 4px -35px 0;" src="{{BASE_URL}}/static/images/red-cross.png"> {% endif %}
    {%if injuary_mark.person.mark2 %}<img style="float: right;margin:5px 8px -35px -8px;" src="{{BASE_URL}}/static/images/red-cross.png"> {% endif %}
{%endfor%}

Last update:

I want to show the details in InjuredLocation models against id in the Reportperson models.This is from a single report,see the Report models in the figure.

我想在Reportperson模型中针对id显示InjuredLocation模型中的详细信息。这是来自单个报告,请参见图中的报告模型。

All the three models with data i pasted below.

以下粘贴数据的所有三个模型。

如何以这种方式过滤数据?如何以这种方式过滤数据?如何以这种方式过滤数据?

What i required as output is,a tab will be dynamically created when a row in InjuredLocation model is created against id in Reportperson table.I want to show the mark from InjuredLocation table against their respective id from Reportperson table in their respective tabs.Now all the mark whatever created against id in Reportperson model are shown in all tabs.Assume mark for id=1 is empty and Marks for id=2 and id=3 are their in database,as per requirement tab1 should not show any data,but now tab1 is showing data of tab2 and tab3 from id=2 and id=3's data.Need help

我需要输出的是,当在Reportperson表中针对id创建InjuredLocation模型中的一行时,将动态创建一个选项卡。我想在InjuredLocation表中显示来自各自标签中Reportperson表中各自ID的标记。现在所有在Reportperson模型中针对id创建的标记显示在所有标签中。对于id = 1的假设标记为空,标记为id = 2和id = 3是它们在数据库中,根据要求tab1不应显示任何数据,但现在tab1显示tab2和tab3的数据,来自id = 2和id = 3的数据。需要帮助

1 个解决方案

#1


1  

Your problem is with this line:

你的问题在于这一行:

injuary_mark = InjuredLocation.objects.filter(pk=reportperson)

The exception refers to a different line because that's where the queryset is actually evaluated.

异常引用不同的行,因为这是实际评估查询集的位置。

There are two problems here.

这里有两个问题。

The fatal one is that reportperson is not a single value - it's a queryset:

致命的一点是,报告人不是一个单一的值 - 它是一个查询集:

reportperson = ReportPerson.objects.filter(report=report_id, action_type="involved")

As you note, 'each report can have multiple names' - and this will find all of them that match the action_type, so it's not an appropriate value to use in an exact lookup.

正如您所指出的,“每个报告可以有多个名称” - 这将找到与action_type匹配的所有报告,因此它不适合在精确查找中使用。

Also, you almost certainly do not mean pk=reportperson - even if reportperson were a single value, you're filtering on the wrong field.

此外,您几乎肯定不是指pk = reportperson - 即使报告人是单一值,您也会过滤错误的字段。

The fix is somewhat dependent on what exactly you want to do with the multiple names. If you just want to get all the InjuredLocation instances that relate to the report_id regardless of report name, this is a more concise expression:

修复程序在某种程度上取决于您对多个名称的确切要求。如果您只想获取与report_id相关的所有InjuredLocation实例,而不管报告名称如何,这是一个更简洁的表达式:

injuries = InjuredLocation.objects.filter(reportperson__report_id=report_id, reportperson__action_type="involved")

If necessary you could use your original reportperson lookup and then an __in filter, but the version above using __ to filter on related values is more concise. In the database, __in uses a subquery while filtering using __ performs a join; the two can have different performance. The __in version would be:

如有必要,您可以使用原始报告人员查找,然后使用__in过滤器,但上面使用__过滤相关值的版本更简洁。在数据库中,__ in使用子查询,而使用__进行过滤则执行连接;两者可以有不同的表现。 __in版本将是:

 reportpeople = ReportPerson.objects.filter(report=report_id, action_type="involved")
 injuries = InjuredLocation.objects.filter(reportperson__in=reportpeople)

If you want to keep each InjuredLocation instance with its ReportPerson instance, say because you're going to group them in a template:

如果您希望将每个InjuredLocation实例与其ReportPerson实例保持一致,请说是因为您要将它们分组到模板中:

reportpeople = ReportPerson.objects.filter(report_id=report_id, action_type="involved")
for reportperson in reportpeople:
    injuries = reportperson.injuredlocation_set.all()
    # now do something with them

Edited:

if you given me a sample how to make the queryset and how to iterate in template will be a great help for me

如果您给我一个示例如何制作查询集以及如何在模板中进行迭代将对我有很大帮助

Something like:

In the view:

在视图中:

reportpeople = ReportPerson.objects.filter(report_id=report_id, action_type="involved")
return render('mytemplate.html', {'reportpeople': reportpeople})

In the template:

在模板中:

{% for reportperson in reportpeople %}
  <p>Report name: {{ reportperson.name }}</p>
  <ul>
    {% for injured_location in reportperson.injuredlocation_set.all %}
      <li>
        {% if injured_location.mark1 %}
          <img style="float: right; margin:5px 4px -35px 0;" src="{{BASE_URL}}/static/images/red-cross.png"> {% endif %}
        {% if injured_location.mark2 %}
          <img style="float: right;margin:5px 8px -35px -8px;" src="{{BASE_URL}}/static/images/red-cross.png"> {% endif %}
      </li>
    {% endfor %}
  </ul>
{% endfor %}

Or whatever HTML you want for each report name. The point is that you can get at the InjuredLocation instances related to a particular ReportPerson instance via the injuredlocation_set manager.

或者您想要的每个报告名称的HTML。关键是您可以通过hurtlocation_set管理器获取与特定ReportPerson实例相关的InjuredLocation实例。

#1


1  

Your problem is with this line:

你的问题在于这一行:

injuary_mark = InjuredLocation.objects.filter(pk=reportperson)

The exception refers to a different line because that's where the queryset is actually evaluated.

异常引用不同的行,因为这是实际评估查询集的位置。

There are two problems here.

这里有两个问题。

The fatal one is that reportperson is not a single value - it's a queryset:

致命的一点是,报告人不是一个单一的值 - 它是一个查询集:

reportperson = ReportPerson.objects.filter(report=report_id, action_type="involved")

As you note, 'each report can have multiple names' - and this will find all of them that match the action_type, so it's not an appropriate value to use in an exact lookup.

正如您所指出的,“每个报告可以有多个名称” - 这将找到与action_type匹配的所有报告,因此它不适合在精确查找中使用。

Also, you almost certainly do not mean pk=reportperson - even if reportperson were a single value, you're filtering on the wrong field.

此外,您几乎肯定不是指pk = reportperson - 即使报告人是单一值,您也会过滤错误的字段。

The fix is somewhat dependent on what exactly you want to do with the multiple names. If you just want to get all the InjuredLocation instances that relate to the report_id regardless of report name, this is a more concise expression:

修复程序在某种程度上取决于您对多个名称的确切要求。如果您只想获取与report_id相关的所有InjuredLocation实例,而不管报告名称如何,这是一个更简洁的表达式:

injuries = InjuredLocation.objects.filter(reportperson__report_id=report_id, reportperson__action_type="involved")

If necessary you could use your original reportperson lookup and then an __in filter, but the version above using __ to filter on related values is more concise. In the database, __in uses a subquery while filtering using __ performs a join; the two can have different performance. The __in version would be:

如有必要,您可以使用原始报告人员查找,然后使用__in过滤器,但上面使用__过滤相关值的版本更简洁。在数据库中,__ in使用子查询,而使用__进行过滤则执行连接;两者可以有不同的表现。 __in版本将是:

 reportpeople = ReportPerson.objects.filter(report=report_id, action_type="involved")
 injuries = InjuredLocation.objects.filter(reportperson__in=reportpeople)

If you want to keep each InjuredLocation instance with its ReportPerson instance, say because you're going to group them in a template:

如果您希望将每个InjuredLocation实例与其ReportPerson实例保持一致,请说是因为您要将它们分组到模板中:

reportpeople = ReportPerson.objects.filter(report_id=report_id, action_type="involved")
for reportperson in reportpeople:
    injuries = reportperson.injuredlocation_set.all()
    # now do something with them

Edited:

if you given me a sample how to make the queryset and how to iterate in template will be a great help for me

如果您给我一个示例如何制作查询集以及如何在模板中进行迭代将对我有很大帮助

Something like:

In the view:

在视图中:

reportpeople = ReportPerson.objects.filter(report_id=report_id, action_type="involved")
return render('mytemplate.html', {'reportpeople': reportpeople})

In the template:

在模板中:

{% for reportperson in reportpeople %}
  <p>Report name: {{ reportperson.name }}</p>
  <ul>
    {% for injured_location in reportperson.injuredlocation_set.all %}
      <li>
        {% if injured_location.mark1 %}
          <img style="float: right; margin:5px 4px -35px 0;" src="{{BASE_URL}}/static/images/red-cross.png"> {% endif %}
        {% if injured_location.mark2 %}
          <img style="float: right;margin:5px 8px -35px -8px;" src="{{BASE_URL}}/static/images/red-cross.png"> {% endif %}
      </li>
    {% endfor %}
  </ul>
{% endfor %}

Or whatever HTML you want for each report name. The point is that you can get at the InjuredLocation instances related to a particular ReportPerson instance via the injuredlocation_set manager.

或者您想要的每个报告名称的HTML。关键是您可以通过hurtlocation_set管理器获取与特定ReportPerson实例相关的InjuredLocation实例。