benoitc/couchdbkit

__set__ always calls __get__

Opened this issue · 2 comments

I have implemented some sort of foreign keys automatic fetching. For this I just created custom Property, that automatically returns related document from database when such property is get. But couchdbkit also always calls get on set operation.

Here is example:

  1. I try to set a value to docref property:

    doc.docref = special_docref_object
    
  2. CouchDbKit uses hasattr(self, key) in setattr, what causes get to be called:

    couchdbkit/schema/base.py(182)__setattr__()
    --> 182             if not hasattr( self, key ) ...
    
  3. Now I get unwanted behaviour - database hit, because get is called, but should not be:

    myapp.py(169)__get__()
    --> 169     db.get(value)
    

Maybe

hasattr(self, key)

can be replaces with:

hasattr(self._properties, key)

?

getattr doesn't only looks in self.properties though. Maybe this can be changed at this point. I syour property code open somewhere so I can eventually try it?

Yes, property class is here:

https://bitbucket.org/sirex/django-sboard/src/517be713b1d7/sboard/models.py#cl-172

But for now, I did a workaround, and made referenced node loading from database only through ref attribute, direct call just returns lazy reference object. So now it solves problem for me.