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()

Package, Architecture, Component, Mirror,Release都等价于:

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

0

自己回答...

找到了基于Django REST框架的解决方案:非模型序列化器

也许可以实施更好的解决方案?

class Pmcra(object):
    package    = None
    package_id = None

    def __init__(self, package, *args, **kw):
        self.package = package
        pass

    def do_work(self):

        my_dict = {}

        if self.package:
            pacmrs = Pacmr.objects.filter(package__name=self.package)
            mirrors = pacmrs.all().distinct('mirror')
            mirrors_array = []
            for mirror in mirrors:
                self.package_id = mirror.package.id
                components = mirrors.filter(mirror=mirror.mirror).distinct('component')
                components_array = []
                for component in components:
                    releases = components.filter(component=component.component).distinct('release')
                    releases_array = []
                    for release in releases:
                        releases_array.append( {'name': release.release.__str__(), 'version': release.version.__str__() })
                    components_array.append( {'name': component.component.__str__(), 'releases': releases_array} )
                mirrors_array.append( {'name': mirror.mirror.__str__(), 'components': components_array} )

            return { 'package': self.package, 'package_id': self.package_id, 'mirrors': mirrors_array }


class MyView(APIView):

    def get(self, request, *args, **kw):
        # Process any get params that you may need
        # If you don't need to process get params,
        # you can skip this part
        get_arg1 = request.GET.get('pkg', None)

        # Any URL parameters get passed in **kw
        myClass = Pmcra(get_arg1, *args, **kw)
        result = myClass.do_work()
        response = Response(result, status=status.HTTP_200_OK)
        return response
于 2013-04-19T14:46:26.447 回答