views:

36

answers:

2

Hello!

I have two models:

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)

When I try to delete a JournalistRequest, It shows warning with a lot of nesting, like

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

All accounts are the same one (ids are same), and all requests are the same one, so I think it becaues of a recursion. But I have no idea how to solve this problem in Django 1.1.1! Can you help me?

+1  A: 

Well you have Account foreign keyed to JournalistRequest, and you also have JournalistRequest foreign keyed to Account. It is probably unnecessary to have foreign keys on both model classes. Removing one of the foreign keys would clear up the circular dependency.

If this is not the case you can override the delete method on one or both of the classes depending on what type of behavior you're looking for.

digitaldreamer
A: 

I thought my goal is clear: to have many requests in one Account, and one of the requests must be the main one. What schema should I use for it?

class Account(models.Model):
    #whatever

class JournalistRequest(models.Model):
    account = models.ForeignKey(Account)
    is_main = models.BooleanField(default=False)
zalew
why is it better (except for the recursion)? All I see is more complicated (and maybe dangerous, if there are several Requests with same account and is_main=true) querying for the main request.
valya