Python 如何在django中创建预先存在的模型的实例?

Python 如何在django中创建预先存在的模型的实例?,python,django,django-models,foreign-keys,Python,Django,Django Models,Foreign Keys,我想在django中创建一个任务执行清单应用程序。假设我有一个清单,上面分配了3项任务。现在,我想拥有同一个清单的多个实例,以便多个用户可以在同一时间从他们自己的清单实例中划掉。假设您有一个清单对象,对于相关的任务对象和用户的外键具有反向关系任务集名为用户的型号 check_list = CheckList.objects.get(pk=foo) # Force execution of the task_set query so that we can copy the objects tas

我想在django中创建一个任务执行清单应用程序。假设我有一个清单,上面分配了3项任务。现在,我想拥有同一个清单的多个实例,以便多个用户可以在同一时间从他们自己的清单实例中划掉。

假设您有一个
清单
对象,对于相关的
任务
对象和
用户
外键
具有反向关系
任务集
名为
用户的型号

check_list = CheckList.objects.get(pk=foo)
# Force execution of the task_set query so that we can copy the objects
tasks = list(check_list.task_set.all())
现在,如果您有一个
用户列表
,您想将此
清单
复制到该列表中

for user in users:
    check_list.pk = None
    check_list.id = None
    check_list.user = user
    check_list.save()  # New record saved to the DB related to the user
    for task in tasks:
        task.pk = None
        task.id = None
        task.check_list = check_list
        task.save()  # New record saved to the DB related to the new check_list

这是文件,谢谢你的快速回复。我会试试这个。很抱歉回复晚了。这个解决办法很有效。再次感谢伊恩。