Django quickly copy (clone) a query result method

In the recent Django project, there is a need to copy records, which is to record the regular inspection table. Under the main task, add a subtask at the time. The task content is the same, that is, the primary key id needs to be changed.
My method is as follows:

def set_sub_inspect_task(taskid, start_time):
    task = inspect_model.InspectTask.objects.get(id=taskid)
    task.id = None
    task.start_time = start_time
    task.parent_task = taskid
    task.sub_task = 1
    task.save()
    return task.id

task.id = NoneLater, for a model instance to be newly added to the database, its id cannot be known before save() is executed, because it needs to calculate how many models are already in the database to calculate its id value. In this case, the point of the task object here has changed , the point task.id = None, which is equivalent to cloning the task = inspect_model.InspectTask.objects.get(id=taskid)query result, adding a new id, and other information is the same.
Reference link:
DJANGO's method of copying records

Guess you like

Origin blog.csdn.net/qq_34663267/article/details/111557897