@@ -355,6 +355,39 @@ def __init__(self, meta=None, **kwargs):
355
355
356
356
super (ObjectBase , self ).__init__ (kwargs )
357
357
358
+ @classmethod
359
+ def __list_fields (cls ):
360
+ """
361
+ Get all the fields defined for our class, if we have an Index, try
362
+ looking at the index mappings as well, mark the fields from Index as
363
+ optional.
364
+ """
365
+ for name in cls ._doc_type .mapping :
366
+ field = cls ._doc_type .mapping [name ]
367
+ yield name , field , False
368
+
369
+ if hasattr (cls .__class__ , '_index' ):
370
+ if not cls ._index ._mapping :
371
+ return
372
+ for name in cls ._index ._mapping :
373
+ # don't return fields that are in _doc_type
374
+ if name in cls ._doc_type .mapping :
375
+ continue
376
+ field = cls ._index ._mapping [name ]
377
+ yield name , field , True
378
+
379
+ @classmethod
380
+ def __get_field (cls , name ):
381
+ try :
382
+ return cls ._doc_type .mapping [name ]
383
+ except KeyError :
384
+ # fallback to fields on the Index
385
+ if hasattr (cls , '_index' ) and cls ._index ._mapping :
386
+ try :
387
+ return cls ._index ._mapping [name ]
388
+ except KeyError :
389
+ pass
390
+
358
391
@classmethod
359
392
def from_es (cls , hit ):
360
393
meta = hit .copy ()
@@ -367,10 +400,10 @@ def from_es(cls, hit):
367
400
data [k ] = v
368
401
369
402
doc = cls (meta = meta )
370
- m = cls ._doc_type .mapping
371
403
for k , v in iteritems (data ):
372
- if k in m and m [k ]._coerce :
373
- v = m [k ].deserialize (v )
404
+ f = cls .__get_field (k )
405
+ if f and f ._coerce :
406
+ v = f .deserialize (v )
374
407
setattr (doc , k , v )
375
408
return doc
376
409
@@ -386,27 +419,15 @@ def __getattr__(self, name):
386
419
try :
387
420
return super (ObjectBase , self ).__getattr__ (name )
388
421
except AttributeError :
389
- if name in self ._doc_type .mapping :
390
- f = self ._doc_type .mapping [name ]
391
- if hasattr (f , 'empty' ):
392
- value = f .empty ()
393
- if value not in SKIP_VALUES :
394
- setattr (self , name , value )
395
- value = getattr (self , name )
396
- return value
422
+ f = self .__get_field (name )
423
+ if hasattr (f , 'empty' ):
424
+ value = f .empty ()
425
+ if value not in SKIP_VALUES :
426
+ setattr (self , name , value )
427
+ value = getattr (self , name )
428
+ return value
397
429
raise
398
430
399
- def __get_field (self , name ):
400
- try :
401
- return self ._doc_type .mapping [name ]
402
- except KeyError :
403
- # fallback to fields on the Index
404
- if hasattr (self , '_index' ) and self ._index ._mapping :
405
- try :
406
- return self ._index ._mapping [name ]
407
- except KeyError :
408
- pass
409
-
410
431
def to_dict (self , skip_empty = True ):
411
432
out = {}
412
433
for k , v in iteritems (self ._d_ ):
@@ -428,26 +449,6 @@ def to_dict(self, skip_empty=True):
428
449
out [k ] = v
429
450
return out
430
451
431
- def __list_fields (self ):
432
- """
433
- Get all the fields defined for our class, if we have an Index, try
434
- looking at the index mappings as well, mark the fields from Index as
435
- optional.
436
- """
437
- for name in self ._doc_type .mapping :
438
- field = self ._doc_type .mapping [name ]
439
- yield name , field , False
440
-
441
- if hasattr (self , '_index' ):
442
- if not self ._index ._mapping :
443
- return
444
- for name in self ._index ._mapping :
445
- # don't return fields that are in _doc_type
446
- if name in self ._doc_type .mapping :
447
- continue
448
- field = self ._index ._mapping [name ]
449
- yield name , field , True
450
-
451
452
def clean_fields (self ):
452
453
errors = {}
453
454
for name , field , optional in self .__list_fields ():
0 commit comments