On Wed, Mar 11, 2009 at 10:32 AM, koranthala <koranth...@gmail.com> wrote:

>
> Hi,
>    I have created an Identity Mapper for my website - which is as
> follows:
>
> class IdentityMap(object):
>    def __getattr__(self, name):
>        if not hasattr(self, name):
>            setattr(self, name, {})
>        return getattr(self, name)
>
>
> MAP = IdentityMap()
>
> class MapManager(models.Manager):
>    def get(self, *args, **kwargs):
>        if kwargs.has_key('mapname'):
>            parm = kwargs.pop('mapname')
>            dval = getattr(MAP, self.model.__name__)
>            if not dval.has_key(parm):
>                dval[parm] = super(MapManager, self).get(*args,
> **kwargs)
>            return dval[parm]
>        else:
>            return super(MapManager, self).get(*args, **kwargs)
>
> #Model which is overriden by all models
> class BaseModel(models.Model):
>    objects = MapManager()
>
>    class Meta:
>        abstract = True
>
>
> #--------------------------------------------------------------------------------
> #
> Everytime a get is issued (due to lazy loading I did not implement the
> same for filter), it first checks whether the given mapname is already
> stored inside MAP, and if not only then will it issue a proper get.
> Mapname is given by the user - he can give any unique name to it.
>
> Now, I wanted to do it for all models. Since User is part of
> django.auth, I overrode User too as given below.
>
> from django.contrib.auth.models import User, UserManager
>
> class MyUserManager(UserManager):
>    def get(self, *args, **kwargs):
>        if kwargs.has_key('mapname'):
>            parm = kwargs.pop('mapname')
>            dval = getattr(MAP, self.model.__name__)
>            if not dval.has_key(parm):
>                dval[parm] = super(MyUserManager, self).get(*args,
> **kwargs)
>            return dval[parm]
>        else:
>            return super(MyUserManager, self).get(*args, **kwargs)
>
> class MyUser(User):
>    objects = MyUserManager()
>
>    class Meta:
>        abstract = True
>
>
> #--------------------------------------------------------------------------------
> #
>
> But when I issued a get on MyUser, it is failing with the following
> error
>
>  File "C:\django\My\..\My\test\test.py", line 164, in UserData
>    data.save(form)
>  File "C:\django\My\..\My\test\models.py", line 361, in save
>    self.user = MyUser.objects.find(form.cleaned_data['user'],
> username=form.cleaned_data['user'])
>  File "C:\django\My\..\My\test\models.py", line 93, in find
>    dval[parm] = self.get(*args, **kwargs)
>  File "C:\Python24\Lib\site-packages\django\db\models\manager.py",
> line 93, in get
>    return self.get_query_set().get(*args, **kwargs)
>  File "C:\Python24\Lib\site-packages\django\db\models\query.py", line
> 337, in get
>    num = len(clone)
>  File "C:\Python24\Lib\site-packages\django\db\models\query.py", line
> 161, in __len__
>    self._result_cache = list(self.iterator())
>  File "C:\Python24\Lib\site-packages\django\db\models\query.py", line
> 281, in iterator
>    for row in self.query.results_iter():
>  File "C:\Python24\Lib\site-packages\django\db\models\sql\query.py",
> line 254, in results_iter
>    for rows in self.execute_sql(MULTI):
>  File "C:\Python24\Lib\site-packages\django\db\models\sql\query.py",
> line 2011, in execute_sql
>    sql, params = self.as_sql()
>  File "C:\Python24\Lib\site-packages\django\db\models\sql\query.py",
> line 362, in as_sql
>    out_cols = self.get_columns(with_col_aliases)
>  File "C:\Python24\Lib\site-packages\django\db\models\sql\query.py",
> line 588, in get_columns
>    col_aliases)
>  File "C:\Python24\Lib\site-packages\django\db\models\sql\query.py",
> line 633, in get_default_columns
>    root_pk = opts.pk.column
> AttributeError: 'NoneType' object has no attribute 'column'
>
> I even tried changing the metaclass, but it does not seem to have any
> effect.
> Can someone help me out?
> >
>
There was a feature just implemented in the development version that is
perfect for this.
http://docs.djangoproject.com/en/dev/ref/models/options/#managed

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
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 
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