本文介绍了在Django数据库中删除子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Costumer和Worker模型。 Costumer继承自User,而Worker继承自Costumer,当我执行

  worker.delete(),

它会删除所有三个表中的所有相关对象,我该怎么办,只会删除Worker表中的数据,剩下的表中的数据仍然存在(假设一个客户不想成为一名工作人员,但他仍然想成为一名客户)

解决方案

我不认为有一种内置的方法来将模型降级为超类,也不能将模型推广为子类。



如果您需要修改原始记录,而不是创建新集合,则可能需要使用喜欢...

  DELETE FROM < the_worker_table_name> WHERE id =< the_worker_object_id> 

...然后重新选择对象作为类型 Costumer 进行进一步处理。


Costumer and Worker models. Costumer inherited from User, and Worker inherited from Costumer, when I do

 worker.delete(),

it will delete all the related object in all three tables, how can I do, it will only delete the data in Worker table, and data in rest of tables remain(assume, one costumer doesnt want be a worker longer, but he still want to be a costumer)

解决方案

I don't think there's a built-in way to 'demote' a model to a superclass, nor to 'promote' a model to a subclass.

If you need to modify the original records, rather than creating a new set, you'll probably have to use a raw query like...

DELETE FROM <the_worker_table_name> WHERE id=<the_worker_object_id>

...then re-select the object as type Costumer for any further processing.

这篇关于在Django数据库中删除子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:12