-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfields.py
53 lines (43 loc) · 1.87 KB
/
fields.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from django.db import models
#TODO Make this non-mongo specific
from pymongo.objectid import ObjectId, InvalidId
class ForeignKey(models.ForeignKey):
__metaclass__ = models.fields.subclassing.SubfieldBase
def db_type(self, connection):
super_type = super(ForeignKey, self).db_type(connection=connection)
if not super_type:
if 'django_mongodb_engine' in connection.settings_dict['ENGINE']:
return 'objectid'
return super_type
def to_python(self, value):
if issubclass(type(value), unicode):
return self.rel.to.objects.get(pk = value)
return value
def get_db_prep_save(self, value, **kwargs):
if isinstance(type(value), type(self.rel.to)):
return unicode(value.pk)
else:
return value
def get_db_prep_lookup(self, *args, **kwargs):
look_up = args[0]
value = args[1]
if value == [] and look_up == 'exact':
# A special case where querying on an empty list filter(some_list=[])
return [[]]
else:
if type(value) == self.rel.to:
# Got a single value
try:
return ObjectId(value.pk)
except InvalidId:
# Provide a better message for invalid IDs
assert isinstance(value, unicode)
if len(value) > 13:
value = value[:10] + '...'
msg = "AutoField (default primary key) values must be strings " \
"representing an ObjectId on MongoDB (got %r instead)" % value
raise InvalidId(msg)
else:
return super(ForeignKey, self).get_db_prep_lookup(*args, **kwargs)
def get_db_prep_value(self, value, connection, prepared=False):
raise NotImplimentedError