Django Map请求主体到模型

时间:2018-08-23 10:32:13

标签: python django

嘿,有可能看到请求匹配的模型/序列化器。

例如,如果我的身体被发送:

  project_id: 1,
  title: "Hello",
  message: "World"

运行类似的代码

matchingModel = AllMyModels.map(request.body)

并使matchingModel类似于Project

1 个答案:

答案 0 :(得分:1)

# mode creation
class Test(models.Model):
    name = models.CharField(max_length = 100)

    class Meta:
        db_table = 'test'
    @classmethod
    def create(cls, **kwargs):
        clazz = cls(**kwargs)
        return clazz

    #create dictionary
    dic = {}
    dic['name'] = 'Test'

    # create object
    t = Test.create(**dic)