【问题标题】:Django : Temporary table or views to create flattened JSONDjango:创建扁平化 JSON 的临时表或视图
【发布时间】:2021-10-03 08:42:33
【问题描述】:

我们可以创建一个临时表或视图来存储来自 3 或 4 个不同表的数据,并将扁平化的 JSON 而不是嵌套的 JSON 发送到 Django 的前端吗?

我的模型是:

class Place(models.Model):
    id = models.IntegerField(primary_key=True)
    location = models.CharField(max_length=100)

    class Meta:
        db_table = 'place'
        managed=False
        
        
class Session(models.Model):
    id = models.IntegerField(primary_key=True)
    place = models.ForeignKey(Place,related_name='session',on_delete=models.CASCADE, null=True)
    start = models.DateField(auto_now=True)
    counts = models.IntegerField()

    class Meta:
        db_table = 'session'
        managed=False


class Animal(models.Model):
    id = models.IntegerField(primary_key=True)
    sess = models.ForeignKey(Session,related_name='details',on_delete=models.CASCADE, null=True)
    type = models.CharField(max_length=100)
    is_active = models.BooleanField()
    length = models.DecimalField(max_digits=6, decimal_places=2)


    class Meta:
        db_table = 'animal'
        managed=False

我正在尝试的扁平化输出是:

[
    {
        "location": "Loc 1",
        "session_start": "2021-01-01",
        "session_count": 900,
        "session_details_id": 1,
        "session_details_length_max": "22.00",
        "session_details_length_min": "10.00",
        "session_details_length_avg": "16.43",
        "session_details_length_std": "16.00",
        "session_details_is_active": false,
        "session_details_type": "dog"

    },
        "location": "Loc 1",
        "session_start": "2021-01-02",
        "session_count": 400,
        "session_details_id": 2,
        "session_details_length_max": "19.00",
        "session_details_length_min": "12.00",
        "session_details_length_avg": "15.43",
        "session_details_length_std": "13.00",
        "session_details_is_active": false,
        "session_details_type": "dog"
    }
]

而不是我目前得到的嵌套 JSON 数据 嵌套的 JSON 数据是

[
    {
        "location": "Loc 1",
        "session": [
            {
                "start": "2021-01-01",
                "count": 600,
                "details": [
                    {
                        "id": 1,
                        "length_max": "15.00",
                        "length_min": "10.00",
                        "length_avg": "12.00",
                        "length_std": "13.00",
                        "is_active": false,
                        "type": "dog"
                    }
                ]
            },
            {
                "start": "2021-01-02",
                "count": 400,
                "details": [
                    {
                        "id": 2,
                        "length_max": "19.00",
                        "length_min": "12.00",
                        "length_avg": "15.00",
                        "length_std": "13.00",
                        "is_active": true,
                        "type": "dog"
                    }
                ]
            },
            {
                "start": "2021-01-01",
                "count": 300,
                "details": [
                    {
                        "id": 13,
                        "length_max": "22.00",
                        "length_min": "20.00",
                        "length_avg": "20.00",
                        "length_std": "22.00",
                        "is_active": false,
                        "type": "dog"
                    }
                ]
            }
        ]
    }
]

像我们在 SQL 中所做的那样,我们可以在 Django 中创建临时表或视图来存储和展平吗?

【问题讨论】:

  • This 可能有帮助

标签: django django-models django-rest-framework django-views django-forms


【解决方案1】:

您不需要额外的表或临时视图即可。

Here 是一个 SerializerMethodField,我猜你需要什么。例如,在您的用例中,您应该为 Animal 模型创建序列化程序,因为它与 Session 模型相关,而 Session 模型与 Place 模型相关,只需在关系中从下到上,并使用 SerializerMethodField 获取其他字段。

一个小例子:

from rest_framework import serializers


class AnimalSerializer(serializers.ModelSerializer):
    location = serializers.SerializerMethodField()  # You can pass the method argument here, of use the default. If you don't pass this argument your method name should be like `get_<field_name>`, for this field `get_location`
    session_details_type = serializers.SerializerMethodField()
    session_count = serializers.SerializerMethodField()

    def get_location(self, obj):
       return obj.session.place.location

   def get_session_details_type(self, obj):
       return obj.type

   def get_session_count(self, obj):
       # Calculate session count in your way and return it.

您可以添加更多这样的字段并实现您想要的。

【讨论】:

  • 谢谢@arif,这样做,性能不会下降吗?数据何时增加以及何时将其调用到前端?在视图中,如果我需要根据位置或日期或任何其他顶级字段进行过滤,这是不可能的,对此有任何建议
  • 如果我需要发布数据,这个不能用,因为只有表中的列是可见的吧?如果有什么好的建议或步骤,欢迎分享。
  • 你可以重写序列化器的create方法,或者创建另一个序列化器来发布数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 2011-11-15
相关资源
最近更新 更多