Django ORM返回多对多键关系中的所有数据

az31mfrm  于 2023-08-08  发布在  Go
关注(0)|答案(2)|浏览(112)

当我在Django模型上应用.filter()时,我就遇到了这个问题,它返回了我不想要的所有销售数据,我试图根据日期过滤数据,只想要输入日期的数据
销售模式:

class Sales(models.Model):
    Amount = models.FloatField(blank=True, null=True)
    Date = models.DateField()
    updated_at = models.DateTimeField(auto_now=True)
    VAT = models.BooleanField(default=True)

字符串
机器型号:

class Machine(models.Model):
    name = models.CharField(max_length=50)
    Sales = models.ManyToManyField(Sales, blank=True)
    ......


API:

class MachineDateData(APIView):
    def post(self, request):
        target_date = date(2023, 5, 1)
        data = Machine.objects.filter(id=524).filter(Sales__Date=target_date)
        serializer = MachineReadOnlySerializer(data, many=True)
        return Response(serializer.data)


串行化器:

class MachineReadOnlySerializer(serializers.ModelSerializer):
    class Meta:
        model = Machine
        depth = 1
        fields = ('id','name', 'Sales')


测试结果:

[
    {
        "id": 524,
        "name": "Résidhome Marseille Saint Charles ",
        "Sales": [
            {
                "id": 67274,
                "Amount": 45.12327285161129,
                "Date": "2023-05-01",
                "updated_at": "2023-07-29T19:08:07.702463Z",
                "VAT": false
            },
            {
                "id": 67275,
                "Amount": 43.24381640146215,
                "Date": "2023-05-02",
                "updated_at": "2023-07-29T19:08:07.725326Z",
                "VAT": false
            },
            {
                "id": 67276,
                "Amount": 85.53046658140136,
                "Date": "2023-05-03",
                "updated_at": "2023-07-29T19:08:07.747431Z",
                "VAT": false
            },
...more results here
}
]


我正在寻找这样的结果,两个表的内部连接,我尝试在SQL上运行,在SQL上运行良好并显示结果,但在Django ORM中它确实显示了所有数据

[
    {
        "id": 524,
        "name": "Résidhome Marseille Saint Charles ",
        "Sales": [
            {
                "id": 67274,
                "Amount": 45.12327285161129,
                "Date": "2023-05-01",
                "updated_at": "2023-07-29T19:08:07.702463Z",
                "VAT": false
            }           
        ]
    }
]


SQL查询

select * from Machine_machine mm INNER JOIN  Machine_machine_Sales as mms on mm.id = mms.machine_id INNER JOIN Machine_sales Ms on mms.sales_id = Ms.id where Date ='2023-05-01';


SQL查询(数据。查询)

SELECT "Machine_machine"."id", "Machine_machine"."name", "Machine_machine"."Modem", "Machine_machine"."Picture", "Machine_machine"."IMEI", "Machine_machine"."Telemetry_Unit_Firmware", "Machine_machine"."Manufacturer_Model", "Machine_machine"."Serial_No", "Machine_machine"."Machine_type_id", "Machine_machine"."FW", "Machine_machine"."Protocol", "Machine_machine"."Connection_Method", "Machine_machine"."Data_usage_last_month", "Machine_machine"."Data_overspend_last_month", "Machine_machine"."Power", "Machine_machine"."Network", "Machine_machine"."Sim_Status", "Machine_machine"."Communication", "Machine_machine"."Signal", "Machine_machine"."Active", "Machine_machine"."Location_id" FROM "Machine_machine" INNER JOIN "Machine_machine_Sales" ON ("Machine_machine"."id" = "Machine_machine_Sales"."machine_id") INNER JOIN "Machine_sales" ON ("Machine_machine_Sales"."sales_id" = "Machine_sales"."id") WHERE ("Machine_machine"."id" = 524 AND "Machine_sales"."Date" = 2023-05-01) ORDER BY "Machine_machine"."id" ASC

hfsqlsce

hfsqlsce1#

首先检查对象数据。我认为对象具有多个销售值关系。

machine = Machine.objects.filter(id=524)
print(machine.Sales.all())

字符串
如果存在,则删除不需要的数据,只保留所需的数据。

afdcj2ne

afdcj2ne2#

4天后,我终于想出了解决这个问题的更好方法!

Machine.objects.prefetch_related(Prefetch('Sales', queryset=Sales.objects.filter(Date__month=7,Date__year=2023)))

字符串
可以基于用于数据过滤值来改变过滤器中的值
现在的数据集:

[
{
    "id": 517,
    "name": "PULV - Nanterre (R+1 Café)",
    "Sales": [
        {
            "id": 105487,
            "Amount": 1.6527446398663428,
            "Date": "2023-07-01",
            "updated_at": "2023-07-29T19:27:15.635027Z",
            "VAT": false
        },
        {
            "id": 105488,
            "Amount": 67.29903784065506,
            "Date": "2023-07-02",
            "updated_at": "2023-07-29T19:27:15.657534Z",
            "VAT": true
        },

相关问题