following miikkas's suggestion re model.Manager, I came up with the following that works for the much simpler case of retrieving the id field by querying uuid. the database was created with the ID being a varchar field used for a hexadecimal string, and I'm retrofitting a sequential integer ID field so I can use Django's auth module which requires it. and I want to do this in steps, hence the hack.
if DEVELOPMENT['merging_to_sequential_ids_incomplete']:
class ModelManager(models.Manager):
def get(self, *args, **kwargs):
if 'uuid' in kwargs:
kwargs['id'] = kwargs.pop('uuid')
return super(ModelManager, self).get(*args, **kwargs)
class Model(models.Model):
if DEVELOPMENT['merging_to_sequential_ids_incomplete']:
print >>sys.stderr, 'WARNING: uuid now a synonym to id'
id = models.CharField(max_length = 32,
primary_key = True, default = uuid_string)
objects = ModelManager() # for Client.objects.get(uuid=...)
uuid = property(lambda self: self.id) # for client.uuid
else:
id = models.AutoField(primary_key = True)
uuid = models.CharField(max_length = 32, ...
now I can:
cd myapp && ../djangopython manage.py shell
WARNING: uuid now a synonym to id
setting up special admin settings
Python 2.7.8 (default, Nov 18 2014, 16:29:10)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from myapp.models import *
>>> Client.objects.get(uuid=u'18b86bd7b58e4c0186f7654045ce81d9')
<Client: [email protected]>
>>> _.uuid
u'18b86bd7b58e4c0186f7654045ce81d9'
filter could be done the same way.
maybe this can help guide someone else looking for a way to use an "alias" or "synonym" for a Django model field. I don't believe it will help the OP though. the custom field type might be the better general approach.