fix unnecessary field-name/model-name conflicts (i.e. field_name == model_name.lower() )
=> __init__ + __getattribute__ now handle only the Django inheritance
references that are needed ('modela_ptr', 'modelc' etc.), avoiding
unnecessary conflicts with field names.
fix_request_path_info
parent
2795f7acd5
commit
7e584632b8
|
|
@ -5,6 +5,7 @@
|
||||||
.project
|
.project
|
||||||
.pydevproject
|
.pydevproject
|
||||||
.settings
|
.settings
|
||||||
|
nbproject
|
||||||
|
|
||||||
tmp
|
tmp
|
||||||
libraries-local
|
libraries-local
|
||||||
|
|
|
||||||
|
|
@ -678,11 +678,11 @@ class PolymorphicModel(models.Model):
|
||||||
def __getattribute__(self, name):
|
def __getattribute__(self, name):
|
||||||
if name != '__class__':
|
if name != '__class__':
|
||||||
#if name.endswith('_ptr_cache'): # unclear if this should be handled as well
|
#if name.endswith('_ptr_cache'): # unclear if this should be handled as well
|
||||||
if name.endswith('_ptr'): name = name[:-4]
|
|
||||||
model = self.__class__.sub_and_superclass_dict.get(name, None)
|
model = self.__class__.sub_and_superclass_dict.get(name, None)
|
||||||
if model:
|
if model:
|
||||||
id = super(PolymorphicModel, self).__getattribute__('id')
|
id = super(PolymorphicModel, self).__getattribute__('id')
|
||||||
attr = model.base_objects.get(id=id)
|
attr = model.base_objects.get(id=id)
|
||||||
|
#print '---',self.__class__.__name__,name
|
||||||
return attr
|
return attr
|
||||||
return super(PolymorphicModel, self).__getattribute__(name)
|
return super(PolymorphicModel, self).__getattribute__(name)
|
||||||
|
|
||||||
|
|
@ -690,23 +690,28 @@ class PolymorphicModel(models.Model):
|
||||||
# containing all model attribute names we need to intercept
|
# containing all model attribute names we need to intercept
|
||||||
# (do this once here instead of in __getattribute__ every time)
|
# (do this once here instead of in __getattribute__ every time)
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
if not getattr(self.__class__, 'sub_and_superclass_dict', None):
|
if not self.__class__.__dict__.get('sub_and_superclass_dict', None):
|
||||||
|
|
||||||
|
def add_if_regular_sub_or_super_class(model, as_ptr, result):
|
||||||
|
if ( issubclass(model, models.Model) and model != models.Model
|
||||||
|
and model != self.__class__ and model != PolymorphicModel):
|
||||||
|
name = model.__name__.lower()
|
||||||
|
if as_ptr: name+='_ptr'
|
||||||
|
result[name] = model
|
||||||
def add_all_base_models(model, result):
|
def add_all_base_models(model, result):
|
||||||
if issubclass(model, models.Model) and model != models.Model:
|
add_if_regular_sub_or_super_class(model, True, result)
|
||||||
result[model.__name__.lower()] = model
|
|
||||||
for b in model.__bases__:
|
for b in model.__bases__:
|
||||||
add_all_base_models(b, result)
|
add_all_base_models(b, result)
|
||||||
def add_all_sub_models(model, result):
|
def add_sub_models(model, result):
|
||||||
if issubclass(model, models.Model) and model != models.Model:
|
|
||||||
result[model.__name__.lower()] = model
|
|
||||||
for b in model.__subclasses__():
|
for b in model.__subclasses__():
|
||||||
add_all_sub_models(b, result)
|
add_if_regular_sub_or_super_class(b, False, result)
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
add_all_base_models(self.__class__, result)
|
add_all_base_models(self.__class__,result)
|
||||||
add_all_sub_models(self.__class__, result)
|
add_sub_models(self.__class__,result)
|
||||||
|
#print '##',self.__class__.__name__,' - ',result
|
||||||
self.__class__.sub_and_superclass_dict = result
|
self.__class__.sub_and_superclass_dict = result
|
||||||
|
|
||||||
super(PolymorphicModel, self).__init__(*args, **kwargs)
|
super(PolymorphicModel, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,9 @@ class BlogA_Entry(ShowFieldsAndTypes, PolymorphicModel):
|
||||||
blog = models.ForeignKey(BlogA)
|
blog = models.ForeignKey(BlogA)
|
||||||
text = models.CharField(max_length=10)
|
text = models.CharField(max_length=10)
|
||||||
|
|
||||||
|
class ModelFieldNameTest(PolymorphicModel):
|
||||||
|
modelfieldnametest = models.CharField(max_length=10)
|
||||||
|
|
||||||
# test bad field name
|
# test bad field name
|
||||||
#class TestBadFieldModel(PolymorphicModel):
|
#class TestBadFieldModel(PolymorphicModel):
|
||||||
# instance_of = models.CharField(max_length=10)
|
# instance_of = models.CharField(max_length=10)
|
||||||
|
|
@ -303,12 +306,17 @@ __test__ = {"doctest": """
|
||||||
>>> type(MROBase2._default_manager)
|
>>> type(MROBase2._default_manager)
|
||||||
<class 'polymorphic.tests.MyManager'>
|
<class 'polymorphic.tests.MyManager'>
|
||||||
|
|
||||||
|
### fixed issue in PolymorphicModel.__getattribute__: field name same as model name
|
||||||
|
>>> ModelFieldNameTest.objects.create(modelfieldnametest='1')
|
||||||
|
<ModelFieldNameTest: id 1, modelfieldnametest (CharField)>
|
||||||
|
|
||||||
### Django model inheritance diamond problem, fails for Django 1.1
|
### Django model inheritance diamond problem, fails for Django 1.1
|
||||||
|
|
||||||
#>>> o=DiamondXY.objects.create(field_b='b', field_x='x', field_y='y')
|
#>>> o=DiamondXY.objects.create(field_b='b', field_x='x', field_y='y')
|
||||||
#>>> print 'DiamondXY fields 1: field_b "%s", field_x "%s", field_y "%s"' % (o.field_b, o.field_x, o.field_y)
|
#>>> print 'DiamondXY fields 1: field_b "%s", field_x "%s", field_y "%s"' % (o.field_b, o.field_x, o.field_y)
|
||||||
#DiamondXY fields 1: field_b "a", field_x "x", field_y "y"
|
#DiamondXY fields 1: field_b "a", field_x "x", field_y "y"
|
||||||
|
|
||||||
|
|
||||||
>>> settings.DEBUG=False
|
>>> settings.DEBUG=False
|
||||||
|
|
||||||
"""}
|
"""}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue