Django: Converting an entire set of a Model's objects into a single dictionary

前端 未结 12 1321
野的像风
野的像风 2020-11-30 16:45

If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.

Is ther

12条回答
  •  没有蜡笔的小新
    2020-11-30 17:50

    Or were you trying to do something like:

    def someview(req):
        models = MyModel.objects.all()
        toTuple = lambda field: (getattr(field, 'someatt'), getattr(field, 'someotheratt'))  
        data = dict(map(toTuple,models))
        return render_to_response(template, data)
    

提交回复
热议问题