Hi,

I did some more research on the differences between using model-
inheritance
and using the "subtypes-way" described in the current documentation,
because after
these mails I was totally unsure which way works best for us.

As a first result from what I understand from satchmo's model
"Product"
one should be very careful when using model inheritance as it
introduces
some possibilities of infinite recursion.

This is caused by the current implementation of some methods which
will
create a loop if one of the subtypes inherits this method from the
class Product. An example is "_get_category" wich calls itself if a
subclass does not overwrite it.

The difference becomes clear if one prints out the __mro__ of the
classes.

For "ConfigurableProduct" this is:

  (<class 'product.modules.configurable.models.ConfigurableProduct'>,
   <class 'django.db.models.base.Model'>, <type 'object'>)

If have created a class "MyFancyProduct" which inherits from
"Product",
this includes "Product" in the method resolution order:

  (<class 'satchmoproducts.myproduct.models.MyFancyProduct'>,
   <class 'product.models.Product'>,
   <class 'django.db.models.base.Model'>, <type 'object'>)


So if one uses "hasattr" to check if an instance of
"MyFancyProduct" has an attribute, this will be "True" for all
attributes which are in "Product". The main problem here is
the implementation of "Product.get_subtype_with_attr" which
uses "hasattr" to check if a subtype has an attribute set.

This means that we would have to use a different way of how to
determine if an attribute is set in a subclass ("MyFancyProduct" in
this case) or in an inherited class like "Product". Maybe this can
be done by checking the "__dict__" attributes of the instance itself
and all elements in "subtype.__class__.__mro__" which are prior
to "Product".


There might also be other problems which arise when using model
inheritance. I am not sure if it is possible to create an instance of
"Product" and later turn it into an instance of "MyFancyProduct".
With the subtypes-approach this is currently possible.

I think the subtypes-approach behaves more like a kind of
annotation object which is attached to Product. Currently it would
not be a problem if a Product has multiple annotations (subtypes)
attached to it.


As far as I understand Django's model inheritance the difference
appears only on the python-level. In the database relations
there should be no difference between subtypes and subclasses of
Product.


My suggestion would be to update the documentation regarding
these issues (only if my conclusions are correct ;-) ) so that
new users will get a clear advice of how to customize product
models.


Johannes


On 30 Nov., 04:24, Stuart Laughlin <[email protected]> wrote:
> Either way will work; it's really just your call. The way Bob Waycott
> shows (i.e. inheritance) is technically 'better' (according to my
> taste anyway), but it's slightly less documented. The 'legacy' way
> (i.e. one-to-one field) is a bit... well, legacy, but it is
> well-documented and therefore perhaps a bit easier. I don't really
> know what else to say about it -- I don't reckon that one way is
> really any more 'correct' than the other. Both ways are pretty quick
> to implement; you might even branch your code, do it both ways, and
> see which you prefer...?

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

Reply via email to