On Feb 26, 12:41 am, Marco Rogers <marco.rog...@gmail.com> wrote:
> Couple of things.
>
> bruno, your save example should maybe look call the superclass save
> first.  Otherwise parent may not have been set yet.

Model.save only do what the name imply : save the instance state (=>
instance attributes values) to the database. It doesn't set the
instance attributes. And if you call the superclass save() method
before doing the test, then the test is useless - the object is
already saved.

> Also, the issue I see with this is how do you know find out which type
> of Node you have so you can query the right model?  For instance when
> you query Node.objects.filter(parent_id=parent_id) you'll get a list
> of Node objects.  But they will not have any of the properties there
> were put on the Group and Item subclasses.

Indeed. Note that the problem at least partly due to the relational
model itself, and there's no easy, obvious solution.

>  Because the Node class
> doesn't know about them.  This is a problem I've had with django for a
> while now and i would love to see a solution that addresses it.

If you only have 2 Node subclasses then there's a very simple Q&D
solution:

class Node(models.Model):
   # other code here
   def downcast(self):
       try:
           return self.group
       except Group.DoesNotExist:
           return self.item


This somehow breaks encapsulation since the base class should not have
knowledge of its subclasses, but in the OP example it should not be a
major problem. And it's indeed not totally transparent. There are
other, more involved solutions - some of them including the
ContentType framework and some trickery with managers and querysets -
but most of them partly break with latest version of the ORM (much to
my regret). As I said, SQL is pretty lacking when it comes to
heterogenous hierarchies.

Anyway: the example I gave was not supposed to be a full working
solution - just a possible starting point.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to