如何在django中像这样组合和加载数据?



这是我的模型

class model1(models.Model):
    name = models.charField()
class model2(models.Model):
    model1 = models.Foriegnkey(model1)
    id = models.IntegerField(primarykey=True)
    title = models.charFiled()

我想要这样的东西

{"model_name1":[{"id":1,"title":"title1"},{"id":2,"title":"title2"}],
 "model_name2":[{"id":3,"title":"title1"},{"id":4,"title":"title2"}],}

如何在django中实现这个,最有效的方法

[
    {
        "model": "app_name.model_name1",
        "fields": { "title": "title1" }
    },
    {
        "model": "app_name.model_name1",
        "fields": { "title": "title2" }
    },
    {
        "model": "app_name.model_name2",
        "fields": { 
            "title": "title1"
            }
    },
    {
        "model": "app_name.model_name2",
        "fields": { 
            "title": "title2"
         }
    }
]

只需在字段中添加列名和它的值。你不需要添加"id",它将由django创建。

相关内容

  • 没有找到相关文章

最新更新