>> class Tracker(models.Model):
>>    notifications = models.ForeignKey(Notification)
>>
>> class Notification(models.Model):
>>     # Common fields
>>     pass
>>
>> class EmailNotification(Notification):
>>    pass
>>
>> class SmsNotification(Notification):
>>    pass
>>
>>
>> # I would be able to iterate over all types of notification
>> for n in tracker.notifications:
>>    print n
> 
> Not trivially, purely in SQL, at the moment. You really need to union
> two sets of results together, since they're querying different tables.
> Lots of history here, but basically, it's a hard problem to solve
> efficiently. One day we might do it.

If one is willing to be roped to PostgreSQL, it does support such 
a scheme with table-inheritance.  You'd have to hack the table 
definitions by hand instead of letting "manage.py syncdb" do it 
for you.  Additionally, Django's ORM knows nothing about this DB 
hack, so virtual methods won't work like one might hope:

   class Notification(models.Model):
     def __unicode__(self):
       return u"Generic notification:" % self.common_field
   class EmailNotification(Notification):
     def __unicode__(self):
       return u"Email: %s" % self.some_email_field
   class SMSNotification(Notification):
     def __unicode__(self):
       return u"SMS: %s" % self.some_sms_field

   for n in tracker.notifications:
     print unicode(s)

You'll end up with every item being treated as a Notification 
object (and thus printing "Generic notification" for all of 
them), rather than getting the notification-specific methods.

> On the other hand, since querysets are iterators, you can construct two
> querysets and then use itertools.chain to combine them and have it
> appears as a single iterator.

A slightly less DRY solution than ideal (ideal=solving the VERY 
complex STI problem in Django's ORM), but more portable across 
database back-ends, less fragile, and available out of the box.

Another alternative might be using Django's built-in generic 
relations.

-tim





--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to