Re: model inheritance - getting data from child objects

2008-11-14 Thread Enrico

Maybe this could help:
http://www.djangosnippets.org/snippets/1187/
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: model inheritance - getting data from child objects

2008-11-08 Thread euglena

Use ContentType. Read about it in django documentation

On Nov 8, 2:03 am, Alistair Marshall <[EMAIL PROTECTED]>
wrote:
> On Nov 7, 4:19 pm, Gerard flanagan <[EMAIL PROTECTED]> wrote:
>
> > I don't know if it's clever or stupid, but this is what I have done in a
> > similar situation:
>
> It appears to be a nice solution - it throws an error If I try to
> get_related() on a generic non-specialised place rather than just
> returning itself, but I don't think that will be a problem for this
> application.
>
> -- update actually this can be fixed with a try except statement:
> #-
> class Item(models.Model):
>      
>    typecode = models.CharField(max_length=30, editable=False)
>
>    def save(self, force_insert=False, force_update=False):
>      self.typecode = self.__class__.__name__.lower()
>      super(Item, self).save(force_insert, force_update)
>
>    def get_related(self):
>     try:
>       return
> self.__class__.__dict__[self.typecode].related.model.objects.get(id=self.id )
>     except:
>       return self
>
> class SubItem(Item):
>      
>
> #-
>
> Thanks
>
> Alistair
>
> --
> Alistair Marshall
>
> www.thatscottishengineer.co.uk
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: model inheritance - getting data from child objects

2008-11-07 Thread Alistair Marshall



On Nov 7, 4:19 pm, Gerard flanagan <[EMAIL PROTECTED]> wrote:
> I don't know if it's clever or stupid, but this is what I have done in a
> similar situation:
>

It appears to be a nice solution - it throws an error If I try to
get_related() on a generic non-specialised place rather than just
returning itself, but I don't think that will be a problem for this
application.

-- update actually this can be fixed with a try except statement:
#-
class Item(models.Model):
 
   typecode = models.CharField(max_length=30, editable=False)

   def save(self, force_insert=False, force_update=False):
 self.typecode = self.__class__.__name__.lower()
 super(Item, self).save(force_insert, force_update)

   def get_related(self):
try:
  return
self.__class__.__dict__[self.typecode].related.model.objects.get(id=self.id)
except:
  return self

class SubItem(Item):
 

#-

Thanks

Alistair

--
Alistair Marshall

www.thatscottishengineer.co.uk
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: model inheritance - getting data from child objects

2008-11-07 Thread Gerard flanagan

Alistair Marshall wrote:
> As I said, I have the function named the same in each child class but
> I need to be able to access it from a list of all the Places.
> 
> My current thinking is just a long list of try, except: statements
> attempting to call the subclass from the Place model but this feels
> hacky and un-clean.
> 

I don't know if it's clever or stupid, but this is what I have done in a 
similar situation:

#-
class Item(models.Model):
 
   typecode = models.CharField(max_length=30, editable=False)

   def save(self, force_insert=False, force_update=False):
 self.typecode = self.__class__.__name__.lower()
 super(Item, self).save(force_insert, force_update)

   def get_related(self):
 return 
self.__class__.__dict__[self.typecode].related.model.objects.get(id=self.id)

class SubItem(Item):
 

#-


If called from an Item instance, 'get_related' will return the related 
SubItem instance.  In your case, you would first get the 'restaurant', 
then call it's 'get_workforce'. Still working on it, so there may be 
unforseen issues.

Regards

G.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: model inheritance - getting data from child objects

2008-11-06 Thread Pawel Pilitowski


On 07/11/2008, at 3:01 AM, Alistair Marshall wrote:


On Nov 6, 2:22 pm, Thomas Guettler <[EMAIL PROTECTED]> wrote:
> If all worker classes (cook, waiter, ...) are subclassed from Worker,
> something like this should work: Worker.objects.filter(workplace=...)
>

Unfortunately the function that each child will run is a bit more
complicated than just listing workers.
It involves output of different equations depending on the class.
Think of it like calculating rents for the different Places and the
rate of rent depends on both the size of the Place and the type of the
place (ie a charity shop might have a lower rate)

As I said, I have the function named the same in each child class but
I need to be able to access it from a list of all the Places.

My current thinking is just a long list of try, except: statements
attempting to call the subclass from the Place model but this feels
hacky and un-clean.

Alistair



Maybe something like this might work:

Store the content type of the child in the parent model.

class Place(models.Model):
 place_type = models.ForeignKey(ContentType)

 def get_workforce(self):
 child = self.place_type.model_class().objects.get 
(place_ptr=self.id)
 child.get_workforce()

Pawel.





--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: model inheritance - getting data from child objects

2008-11-06 Thread Alistair Marshall

On Nov 6, 2:22 pm, Thomas Guettler <[EMAIL PROTECTED]> wrote:
> If all worker classes (cook, waiter, ...) are subclassed from Worker,
> something like this should work: Worker.objects.filter(workplace=...)
>

Unfortunately the function that each child will run is a bit more
complicated than just listing workers.
It involves output of different equations depending on the class.
Think of it like calculating rents for the different Places and the
rate of rent depends on both the size of the Place and the type of the
place (ie a charity shop might have a lower rate)

As I said, I have the function named the same in each child class but
I need to be able to access it from a list of all the Places.

My current thinking is just a long list of try, except: statements
attempting to call the subclass from the Place model but this feels
hacky and un-clean.

Alistair

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: model inheritance - getting data from child objects

2008-11-06 Thread Thomas Guettler

I have never used model inheritance, but I read the documentation

If all worker classes (cook, waiter, ...) are subclassed from Worker,
something like this should work: Worker.objects.filter(workplace=...)

(Given that workers only work for one workplace)

HTH,
  Thomas

Alistair Marshall schrieb:
> Using the example in the django writing models documentation [1] I
> wish to get a list of all the places, then calculate a value that
> depends on what the type of place it is.
>
> Say in my restraunt I have a function get_workforce() which returns a
> list of all the people that work in the restaraunt. This collects all
> the waiters, cooks and managers that are associated with the
> restraunt.
> Then I have another type of place, a shop which also has a function
> get_workforce() which returns a list of people that work in the shop.
> This collects all the shop_assistants and managers associated with the
> shop.
>
> The function is different depending on the type of subclass but I need
> to be able to get access to it from a list of all the places.
>
> Has anyone come across this before and come up with a solution.
>
> Thanks
> Alistair
>
> [1] http://docs.djangoproject.com/en/dev/topics/db/models/#id6
> >
>   


-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



model inheritance - getting data from child objects

2008-11-06 Thread Alistair Marshall

Using the example in the django writing models documentation [1] I
wish to get a list of all the places, then calculate a value that
depends on what the type of place it is.

Say in my restraunt I have a function get_workforce() which returns a
list of all the people that work in the restaraunt. This collects all
the waiters, cooks and managers that are associated with the
restraunt.
Then I have another type of place, a shop which also has a function
get_workforce() which returns a list of people that work in the shop.
This collects all the shop_assistants and managers associated with the
shop.

The function is different depending on the type of subclass but I need
to be able to get access to it from a list of all the places.

Has anyone come across this before and come up with a solution.

Thanks
Alistair

[1] http://docs.djangoproject.com/en/dev/topics/db/models/#id6
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---