Django在删除对象时显示太多警告

时间:2010-04-27 15:05:04

标签: django django-models django-admin

我有两种模式:

class Account(models.Model):
    main_request = models.ForeignKey('JournalistRequest', related_name='main_request')
    key = models.CharField(_('Key'), max_length=100)

class JournalistRequest(models.Model):
    account = models.ForeignKey(Account, blank=True, null=True)

当我尝试删除JournalistRequest时,它会显示带有大量嵌套的警告,例如

Are you sure you want to delete the selected Заявка СМИ objects? All of the following objects and their related items will be deleted:

   Journalist Request: some request
        Account: some account
            Journalist Request: some request
                Account: some account
                    Journalist Request: some request
                        Account: some account
                            Journalist Request: some request
                                Account: some account
                                    Journalist Request: some request

所有帐户都是相同的(ID是相同的),并且所有请求都是相同的,所以我认为这是因为递归。但我不知道如何在Django 1.1.1中解决这个问题!你能救我吗?

2 个答案:

答案 0 :(得分:1)

您的帐户外键是否记入了JournalistRequest,并且您还将JournalistRequest外键键入帐户。可能没有必要在两个模型类上都有外键。删除其中一个外键将清除循环依赖关系。

如果不是这种情况,您可以在一个或两个类上override the delete method,具体取决于您要查找的行为类型。

答案 1 :(得分:0)

  

我认为我的目标很明确:拥有   一个帐户中的许多请求和一个   请求必须是主要的。   我应该使用什么模式?

class Account(models.Model):
    #whatever

class JournalistRequest(models.Model):
    account = models.ForeignKey(Account)
    is_main = models.BooleanField(default=False)