How do I use a dictionary to update fields in Django models?

前端 未结 5 1815
盖世英雄少女心
盖世英雄少女心 2020-12-23 02:21

Suppose I have a model like this:

class Book(models.Model):
    num_pages = ...
    author = ...
    date = ...

Can I create a dictionary,

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-23 02:51

    if you have already Django object and you want to update it's field, you may do it without filter. because you have it already, in this case, yoy may :

    your_obj.__dict__update(your_dict)
    your_obj.save()
    

提交回复
热议问题