[EMAIL PROTECTED] wrote:
> I have searched through the dev list and couldn't find anything
> relating to the specific thing I am trying to do. I also am aware that
> both QuerySet and OneToOne fields are in some sort of flux right now.
> 
> QuerySet is being completely refactored, and I will of course wait for
> that (like my admin changes) to drop before trying to come up with a
> patch. OneToOne field has also been pending changes for about forever
> now and I don't know exactly what's going on with it. So this post is
> to start a dialog about whether we can follow OneToOne relationships
> in a bi-directional manner and what the current plans are.

I like this idea a lot, it's definitely something that's been a 
hindrance on things I'm working on.  OneToOneFields are special, they 
seem like they should provide the relationship whichever way I need to 
(since by its very nature, all this stuff could have just been in one 
table, so I'm just isolating groups of data that are still single 
row-centric into multiple tables).  Use this example:

class Account(models.Model):
        username = models.CharField(maxlength=20)
        password = models.CharField(maxlength=20)
        active = models.BooleanField()

class AccountPersonInfo(models.Model):
        account = OneToOneField(Account)
        first_name = models.CharField(maxlength=20)
        last_name = models.CharField(maxlength=20)

class AccountCompanyInfo(models.Model):
        account = OneToOneField(Account)
        company_name = models.CharField(maxlength=20)
        company_address = models.CharField(maxlength=20)


Now, there's no possible *.objects.select_related() I can use that will 
get me the information in all three of these tables.

* Account.objects.select_related() - Only gives me Account, not 
AccountPersonInfo or AccountCompanyInfo
* AccountPersonInfo.objects.select_related() - Only gives me Account and 
AccountPersonInfo, not AccountCompanyInfo
* AccountCompanyInfo.objects.select_related() - Only gives me Account 
and AccountCompanyInfo, not AccountPersonInfo

I can get any two, but not all three.  If I want the third, it requires 
a second hit to the database.  If I'm selecting one record, that's not 
so bad.  But when I do this:

Account.objects.select_related().filter(active=True)

If I want the information in AccountPersonInfo *and* AccountCompanyInfo 
for the N (for argument, say hundreds of) records it returns, it will do 
that N rows worth of queries for the other table.  This is a performance 
destroyer.

So, my alternatives are:

1) Run 2-3 queries, pulling the linked records back, and then merging 
them on the Python side.  UGLY.
2) Just use a hand-coded query.  Also UGLY, since the Models have 
methods on them that I'd like to use, and populating them by hand from 
the results of the QuerySet is more than a minor annoyance.
3) Make OneToOneFields bidirectional.

For obvious reasons, I think (3) is the right choice.  Is there anything 
that would prevent QuerySet or the Models from working this way?

Thanks,
George

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to