Python 如何使用一组项作为模型中的值来创建字典

Python 如何使用一组项作为模型中的值来创建字典,python,dictionary,django-models,Python,Dictionary,Django Models,我是python的新手,希望能得到一些帮助 我有两个模型,每个模型都有字段,我想用它们作为我正在构建的字典的“值”。第二类文件是第一类文件的子表。我想把模型中的值作为item_集传递 class One(Models.model): id = models.Autofield(Primary_key=True, null=True) type = models.CharField(max_length=50, null=True) name = models.CharF

我是python的新手,希望能得到一些帮助

我有两个模型,每个模型都有字段,我想用它们作为我正在构建的字典的“值”。第二类文件是第一类文件的子表。我想把模型中的值作为item_集传递

class One(Models.model):

    id = models.Autofield(Primary_key=True, null=True)
    type = models.CharField(max_length=50, null=True)
    name = models.CharField(max_length=100, null=True)

class Two(Models.model): 

    two_id = models.ForeignKey(One, blank=False)
    x_coord = models.IntegerField(null=True, blank=True)
    y_coord = models.IntegerField(null=True, blank=True)
    fontsize = models.IntegerField(null=True, blank=True)
从这两个模型中,我想创建一个字典,将所有字段作为一个“集合”进行计数,并创建一个字典,将模型中的值分出来

例如:

它似乎不起作用。希望得到一些急需的帮助。最后我必须将这些值返回给一个主类


Thx

对于dict值,您的意思是:1.id,1.type,1.name,2.two_id???文本“one{id}”看起来不像有效的python。哦,我试图用大括号替换模型类中的值?只需使用一个.id和一个.type等即可?您打算如何表示一个具有0个或多个与其关联的
2个
s?对不起,Ignacio,我不理解您的问题。你能给我举个例子吗?你的意思是当我有命名相同的变量时?
def get_one_two(self, three)

one_two_three = One.objects.using("one_two").select_related("one_two_item_set").filter(three=three)
    one_count = 0
    for i in one_two_three:
        template_dict = {
            'id': one{id},
            'type': one{type},
            'name': one{name},
            'id': two{two_id},
            'x_coord': two{x_coord},
            'y_coord': two{y_coord},
            'fontsize': two{fontsize}
                    }