Bulk create model objects in django

后端 未结 8 1984
执笔经年
执笔经年 2020-12-13 05:17

I have a lot of objects to save in database, and so I want to create Model instances with that.

With django, I can create all the models instances, with MyMode

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 06:11

    for a single line implementation, you can use a lambda expression in a map

    map(lambda x:MyModel.objects.get_or_create(name=x), items)
    

    Here, lambda matches each item in items list to x and create a Database record if necessary.

    Lambda Documentation

提交回复
热议问题