Source

djangoappengine / fields.py

Full commit
from django.db import models
from google.appengine.api.datastore import Key
import logging

class GAEKeyField(models.AutoField):
    description = "A field for Google AppEngine Key objects"
    __metaclass__ = models.SubfieldBase

    def __init__(self, *args, **kwargs):
        kwargs['null'] = True
        super(GAEKeyField, self).__init__(*args, **kwargs)

    def get_internal_type(self):
        return self.__class__.__name__

    def get_related_internal_type(self):
        return self.get_internal_type()

    def to_python(self, value):
        logging.info("to python value: %s %s" % (value, type(value)))
        if isinstance(value, Key):
            return value
        if value is None or len(value) < 1:
            return
        return Key(value)

    def get_prep_value(self, value):
        logging.info("prep value: %s %s" % (value, type(value)))
        
        if isinstance(value, Key):
            return value

        if isinstance(value, (str, basestring)):
            return Key(value)

        return None

    def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
        if not prepared:
            value = self.get_prep_lookup(lookup_type, value)
        
        return value