natural keys for auth.user and group

687 views
Skip to first unread message

Rainy

unread,
Mar 16, 2011, 4:19:40 PM3/16/11
to Django users
Hi, I know natural keys possibly will be added to auth.user and group
in 1.4. I'm using 1.2 currently and I'm trying to add them
dynamically:


class UserManager(models.Manager):
def get_by_natural_key(self, username, first_name):
return self.get(username=username, first_name=first_name)

class GroupManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)

def unatural_key(self):
return (self.username, self.first_name)

def gnatural_key(instance):
return (self.name,)

User.objects = UserManager()
Group.objects = GroupManager()
User.natural_key = unatural_key
Group.natural_key = gnatural_key


It seems like it should work.. (?) but I get this error when trying
to serialize using natural keys:


Traceback (most recent call last):
File "data.py", line 362, in <module>
Backup().main()
File "data.py", line 333, in main
if options.dump : self.do_dump(default_datafile,
self.classes)
File "data.py", line 232, in do_dump
use_natural_keys=True)
File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/
__init__.py", line 87, in serialize
s.serialize(queryset, **options)
File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/
base.py", line 39, in serialize
for obj in queryset:
File "/usr/local/lib/python2.6/dist-packages/django/db/models/
query.py", line 106, in _result_iter
self._fill_cache()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/
query.py", line 760, in _fill_cache
self._result_cache.append(self._iter.next())
File "/usr/local/lib/python2.6/dist-packages/django/db/models/
query.py", line 230, in iterator
fields = self.model._meta.fields
AttributeError: 'NoneType' object has no attribute '_meta'



Is there a way to do this without patching django?

thanks, -ak

Rainy

unread,
Mar 17, 2011, 3:22:34 PM3/17/11
to Django users
I fixed the earlier issue, which was happening during
serialization, by using add_to_class() class method,
but now I'm running into an issue with deserialize():

[...]

for deobj in deserialize(fmt, val, ensure_ascii=False,
use_natural_keys=True):
File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/
json.py", line 38, in Deserializer
for obj in PythonDeserializer(simplejson.load(stream), **options):
File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/
python.py", line 127, in Deserializer
data[field.attname] =
field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/
__init__.py", line 471, in to_python
raise exceptions.ValidationError(self.error_messages['invalid'])
django.core.exceptions.ValidationError: [u'This value must be an
integer.']

This happens on a user value that's a natural key; in serializers/
python.py,
the code looks for get_by_natural_key() method, does not find it and
tries
to process the id as int, which causes this exception.

How can it be that dynamically updated manager works fine on
serialization but not on deserialize?

Thanks, any hints / ideas are appreciated. -ak

Simon Williams

unread,
Oct 27, 2011, 8:28:03 AM10/27/11
to django...@googlegroups.com
Unfortunately, according to the Django ticket for this (https://code.djangoproject.com/ticket/13914) it seems that we aren't going to get this functionality built-in any time soon. However, after many hours of inspection in the Django source, I think I have found a solution...

1. In Django 1.3 the User model already has it's own UserManager, so we must update that instead of overwriting it. This makes adding natural keys for Users much easier.
2. The ValidationError is because it's not even trying to use the natural keys- this code uses private fields like _default_manager, which were initialised before the manager was replaced. In the end, since we are only adding a method and nothing else, the most appropriate way to fix this is to change the __class__ of the objects member in Group.
3. I don't appear to need add_to_class(), but YMMV. An example would be good.

def uget_by_natural_key(self, username):
    return self.get(username=username)
def gnatural_key(self):
        return self.name
def unatural_key(self):
        return self.username

class GroupManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name=name)

UserManager.get_by_natural_key = uget_by_natural_key
Group.objects.__class__ = GroupManager

User.natural_key = unatural_key
Group.natural_key = gnatural_key

For reference, I put this code in models.py.

Disclaimer: This kind of hackery may have serious side-effects, especially since Django has done a lot of initialisation by the time we get to this point.

Anna Warzecha

unread,
Nov 14, 2011, 8:27:44 AM11/14/11
to Django users
Hi,
your above example helped me a lot, thank you! But there is a small
error. natural_key must always return a tuple.
So this part:

def gnatural_key(self):
       return self.name
def unatural_key(self):
       return self.username

Has to be changed:

def gnatural_key(self):
return (self.name,)
def unatural_key(self):
return (self.username,)

And this works perfectly fine.

Thanks again,
Anna Warzecha
Reply all
Reply to author
Forward
0 new messages