我有以下型号
class Database(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class DatabaseUser(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=100)
password = models.CharField(max_length=100)
database = models.ManyToManyField(Database)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
一个DatabaseUser可以控制许多数据库。
如果我要删除数据库它想要删除DatabaseUser,我有这个问题..有没有办法阻止这种情况轻易发生?
答案 0 :(得分:1)
目前尚不清楚为什么你有多个用户的fks,但除此之外你需要解开数据库和任何DatabaseUsers之间的关系
for db_user in yourdatabase.databaseuser_set.all(): #for all users who are linked to the DB you're about to kill
db_user.database.remove(yourdatabase) #unhook the m2m
yourdatabase.delete()
[编辑错误修正]