Django:将整个模型对象集转换为单个字典 [英] Django: Converting an entire set of a Model's objects into a single dictionary

查看:34
本文介绍了Django:将整个模型对象集转换为单个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您从 Google 来到这里寻找要听写的模型,请跳过我的问题,直接跳到第一个答案.我的问题只会让你感到困惑.

在 Django 中有没有一种好方法可以将整个模型的对象集整合到一个字典中?我的意思是,像这样:

Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:

class DictModel(models.Model):
    key = models.CharField(20)
    value = models.CharField(200)


DictModel.objects.all().to_dict()

... 结果是一个字典,其中键/值对由模型中的记录组成?有没有其他人认为这对他们有用?

... with the result being a dictionary with the key/value pairs made up of records in the Model? Has anyone else seen this as being useful for them?

谢谢.

更新
我只想补充一点,我的最终目标是能够在模板中进行简单的变量查找.类似的东西:

Thanks.

Update
I just wanted to add is that my ultimate goal is to be able to do a simple variable lookup inside a Template. Something like:

{{ DictModel.exampleKey }}

结果是 DictModel.objects.get(key__exact=exampleKey).value

With a result of DictModel.objects.get(key__exact=exampleKey).value

不过,总的来说,你们的所有回复都非常有帮助,而且处理方法的不同之处真的让我感到惊讶.非常感谢.

2011 年 10 月更新:如果你在谷歌django model_to_dict",这个问题是最好的结果,这实际上非常糟糕,因为它解决了与我问的不同的问题.

我想要的是能够将查询集中的所有实例映射到以指定模型字段作为键的单个字典中.
model_to_dict,另一方面将单个模型实例转换为字典.

现在,我当时的需求非常具体,可能非常罕见(我什至不记得我需要它的项目,或者为什么).所以我会非常惊讶,任何寻找有关 model_to_dict 信息的人都会发现我的问题实际上很有用.对不起.

model_to_dict 似乎是一个比我更常见的用例.

2011 年 12 月更新:
我更改了标题,希望能更好地反映我的初衷.

Overall, though, you guys have really surprised me with how helpful allof your responses are, and how different the ways to approach it can be. Thanks a lot.

Update October 2011: This question is the top result if you Google "django model_to_dict", which is actually pretty awful given that it solves a different problem than what I was asking.

What I wanted was to be able to map all of the instances in a queryset into a single dictionary with a specified model field as the key.
model_to_dict, on the other hand converts a single model instance into a dictionary.

Now, my needs at the time were pretty darn specific, and probably extremely rare (I can't even remember the project I needed it for, or why). So I would be pretty surprised that anyone looking for information about model_to_dict is going to find my question actually useful. Sorry.

model_to_dict seems to be a much more common usage case than I had.

Update Dec 2011:
I changed the title to hopefully better reflect my original intent.

推荐答案

这需要创建一个实际的 dict 吗?你可以只用看起来像字典的东西吗?

Does this need to create an actual dict? could you get by with only something that looked like a dict?

class DictModelAdaptor():
    def __init__(self, model):
        self.model = model

    def __getitem__(self, key):
        return self.model.objects.get(key=key)

    def __setitem__(self, key, item):
        pair = self.model()
        pair.key = key
        pair.value = item
        pair.save()

    def __contains__(self, key):
        ...

然后你可以用这种方式包装一个模型:

You could then wrap a model in this way:

modelDict = DictModelAdaptor(DictModel)
modelDict["name"] = "Bob Jones"

等等...

这篇关于Django:将整个模型对象集转换为单个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆