1

Jsonを使用してネストされた出力を生成しようとしていdjango-rest-frameworkます。でも正しい道が見つからない…

誰かが私を正しい方向に連れて行ってくれませんか? どうもありがとう!

--

ここに私のモデルがあります:

class Pacmr(models.Model):
    package         = models.ForeignKey(Package)
    architecture    = models.ForeignKey(Architecture)
    component       = models.ForeignKey(Component)
    mirror          = models.ForeignKey(Mirror)
    release         = models.ForeignKey(Release)
    version         = DebVersionField()

PackageArchitectureComponentMirrorReleaseすべて次と同等です。

class Package(models.Model):
    name    = models.CharField(max_length=50)
    comment = models.CharField(max_length=200)

Pacmrテーブル コンテンツの例:

SELECT id, package_id, architecture_id, component_id, mirror_id, release_id, version from pacmr_pacmr where package_id = 20671;

   id   | package_id | architecture_id | component_id | mirror_id | release_id |   version
--------+------------+-----------------+--------------+-----------+------------+--------------
 965201 |      20671 |             236 |          146 |        73 |        111 | 3.0-2.1b-1
 965200 |      20671 |             236 |          146 |        73 |        109 | 3.0-2.1b-2.1
 965199 |      20671 |             236 |          146 |        73 |        113 | 3.0-2.1a-5
 691277 |      20671 |             236 |          146 |        73 |        105 | 3.0-2.1b-3
 691276 |      20671 |             236 |          146 |        73 |        107 | 3.0-2.1b-3
 691275 |      20671 |             236 |          146 |        73 |        104 | 3.0-2.1b-3
 691274 |      20671 |             236 |          146 |        75 |        113 | 3.0-2.1a-5
 691273 |      20671 |             236 |          146 |        72 |        111 | 3.0-2.1b-1
 691272 |      20671 |             236 |          146 |        72 |        110 | 3.0-2.1b-1
 691271 |      20671 |             236 |          146 |        72 |        109 | 3.0-2.1b-2.1
 691270 |      20671 |             236 |          146 |        72 |        108 | 3.0-2.1b-2.1
 691269 |      20671 |             236 |          146 |        72 |        113 | 3.0-2.1a-5
 691268 |      20671 |             236 |          146 |        72 |        115 | 3.0-2.1a-5
 691267 |      20671 |             236 |          146 |        72 |        107 | 3.0-2.1b-3
 691266 |      20671 |             236 |          146 |        72 |        106 | 3.0-2.1b-3
 691265 |      20671 |             236 |          146 |        72 |        104 | 3.0-2.1b-3
 691264 |      20671 |             236 |          146 |        72 |        103 | 3.0-2.1b-3
 691263 |      20671 |             236 |          144 |        71 |         97 | 3.0-2.1b-2.1
 691262 |      20671 |             236 |          144 |        71 |         67 | 3.0-2.1b-3
 691261 |      20671 |             236 |          144 |        71 |        114 | 3.0-2.1b-3
 691260 |      20671 |             236 |          144 |        71 |         68 | 3.0-2.1b-3
 691259 |      20671 |             236 |          144 |        71 |        101 | 3.0-2.1b-3
 691258 |      20671 |             236 |          144 |        71 |        100 | 3.0-2.1b-3
 691257 |      20671 |             236 |          144 |        70 |         97 | 3.0-2.1b-2.1
 691256 |      20671 |             236 |          144 |        70 |         98 | 3.0-2.1b-2.1
 691255 |      20671 |             236 |          144 |        70 |         67 | 3.0-2.1b-3
 691254 |      20671 |             236 |          144 |        70 |         99 | 3.0-2.1b-3
 691253 |      20671 |             236 |          144 |        70 |         68 | 3.0-2.1b-3
 691252 |      20671 |             236 |          144 |        70 |         96 | 3.0-2.1b-3

結合すると、次のようになります。

SELECT pacmr_pacmr.id as id, pacmr_package.name as package, pacmr_architecture.name as architecture, pacmr_component.name as component, pacmr_mirror.name as mirror, pacmr_release.name as release, version from pacmr_pacmr, pacmr_package, pacmr_architecture, pacmr_component, pacmr_mirror, pacmr_release where package_id=pacmr_package.id and architecture_id=pacmr_architecture.id and component_id=pacmr_component.id and mirror_id=pacmr_mirror.id and release_id=pacmr_release.id and package_id = 20671;

   id   | package | architecture | component |      mirror       |     release     |   version
--------+---------+--------------+-----------+-------------------+-----------------+--------------
 965201 | jvim    | source       | main      | debian            | etch            | 3.0-2.1b-1
 965200 | jvim    | source       | main      | debian            | lenny           | 3.0-2.1b-2.1
 965199 | jvim    | source       | main      | debian            | sarge           | 3.0-2.1a-5
 691277 | jvim    | source       | main      | debian            | sid             | 3.0-2.1b-3
 691276 | jvim    | source       | main      | debian            | squeeze         | 3.0-2.1b-3
 691275 | jvim    | source       | main      | debian            | wheezy          | 3.0-2.1b-3
 691274 | jvim    | source       | main      | debian-amd64      | sarge           | 3.0-2.1a-5
 691273 | jvim    | source       | main      | debian-production | etch            | 3.0-2.1b-1
 691272 | jvim    | source       | main      | debian-production | etch-testing    | 3.0-2.1b-1
 691271 | jvim    | source       | main      | debian-production | lenny           | 3.0-2.1b-2.1
 691270 | jvim    | source       | main      | debian-production | lenny-testing   | 3.0-2.1b-2.1
 691269 | jvim    | source       | main      | debian-production | sarge           | 3.0-2.1a-5
 691268 | jvim    | source       | main      | debian-production | sarge-testing   | 3.0-2.1a-5
 691267 | jvim    | source       | main      | debian-production | squeeze         | 3.0-2.1b-3
 691266 | jvim    | source       | main      | debian-production | squeeze-testing | 3.0-2.1b-3
 691265 | jvim    | source       | main      | debian-production | wheezy          | 3.0-2.1b-3
 691264 | jvim    | source       | main      | debian-production | wheezy-testing  | 3.0-2.1b-3
 691263 | jvim    | source       | universe  | ubuntu            | hardy           | 3.0-2.1b-2.1
 691262 | jvim    | source       | universe  | ubuntu            | lucid           | 3.0-2.1b-3
 691261 | jvim    | source       | universe  | ubuntu            | oneiric         | 3.0-2.1b-3
 691260 | jvim    | source       | universe  | ubuntu            | precise         | 3.0-2.1b-3
 691259 | jvim    | source       | universe  | ubuntu            | quantal         | 3.0-2.1b-3
 691258 | jvim    | source       | universe  | ubuntu            | raring          | 3.0-2.1b-3
 691257 | jvim    | source       | universe  | ubuntu-production | hardy           | 3.0-2.1b-2.1
 691256 | jvim    | source       | universe  | ubuntu-production | hardy-testing   | 3.0-2.1b-2.1
 691255 | jvim    | source       | universe  | ubuntu-production | lucid           | 3.0-2.1b-3
 691254 | jvim    | source       | universe  | ubuntu-production | lucid-testing   | 3.0-2.1b-3
 691253 | jvim    | source       | universe  | ubuntu-production | precise         | 3.0-2.1b-3
 691252 | jvim    | source       | universe  | ubuntu-production | precise-testing | 3.0-2.1b-3
(29 rows)

ここに私が持ちたいものの例django-rest-framework(手書き):

{
    "package_id": 20671,
    "package": "jvim",
    "mirrors": [
        {
            "name": "debian",
            "components": [
                {
                    "name": "main",
                    "releases": [
                        {
                            "name": "etch",
                            "version": "3.0-2.1b-1"
                        },
                        {
                            "name": "lenny",
                            "version": "3.0-2.1b-2.1"
                        },
                        {
                            "name": "sarge",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "sid",
                            "version": "3.0-2.1b-3"
                        },
                        {
                            "name": "squeeze",
                            "version": "3.0-2.1b-3"
                        },
                        {
                            "name": "wheezy",
                            "version": "3.0-2.1b-3"
                        }
                    ]
                }
            ]
        },
        {
            "name": "debian-amd64",
            "components": [
                {
                    "name": "main",
                    "releases": [
                        {
                            "name": "sarge",
                            "version": "3.0-2.1a-5"
                        }
                    ]
                }
            ]
        },
        {
            "name": "debian-production",
            "components": [
                {
                    "name": "main",
                    "releases": [
                        {
                            "name": "etch",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "etch-testing",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "lenny",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "lenny-testing",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "sarge",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "sarge-testing",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "squeeze",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "squeeze-testing",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "wheezy",
                            "version": "3.0-2.1a-5"
                        },
                        {
                            "name": "wheezy-testing",
                            "version": "3.0-2.1a-5"
                        }
                    ]
                }
            ]
        },
        {
            "name": "ubuntu",
            "components": [
                {
                    "name": "universe",
                    "releases": [
                        {
                            "name": "hardy",
                            "version": "3.0-2.1b-2.1"
                        },
                        {
                            "name": "lucid",
                            "version": "3.0-2.1b-3"
                        },
                        {
                            "name": "oneiric",
                            "version": "3.0-2.1b-3"
                        },
                        {
                            "name": "precise",
                            "version": "3.0-2.1b-3"
                        },
                        {
                            "name": "quantal",
                            "version": "3.0-2.1b-3"
                        },
                        {
                            "name": "raring",
                            "version": "3.0-2.1b-3"
                        }
                    ]
                }
            ]
        },
        {
            "name": "ubuntu-production",
            "components": [
                {
                    "name": "universe",
                    "releases": [
                        {
                            "name": "hardy",
                            "version": "3.0-2.1b-2.1"
                        },
                        {
                            "name": "hardy-testing",
                            "version": "3.0-2.1b-2.1"
                        },
                        {
                            "name": "lucid",
                            "version": "3.0-2.1b-3"
                        },
                        {
                            "name": "lucid-testing",
                            "version": "3.0-2.1b-3"
                        },
                        {
                            "name": "precise",
                            "version": "3.0-2.1b-3"
                        },
                        {
                            "name": "precise-testing",
                            "version": "3.0-2.1b-3"
                        }
                    ]
                }
            ]
        }
    ]
}
4

1 に答える 1