mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

? Is there a way to tell if refernce field has been de-referenced?

Open mattotodd opened this issue 13 years ago • 1 comments

What triggers mongoengine to de-reference a document? Is there a way to see if a particular reference attribute has been de-referenced?

for example. say i had a document Foo that has a property "bar" that is a reference to a Bar document.

Foo has a method on it "has_valid_bar" that checks some small property buried inside the Bar document.

Say that Bar is a fairly big document, and somewhere is my buisness logic it may have already been de-referenced, I would used the Bar object to calculate whether it's a valid bar for that function, BUT if it hasnt been de-referenced and I call 'has_valid_bar', i dont really need to de-reference the whole Bar object, I can make a more optimized db call to perform that check.

Is there any way to determine if it has or has not been de-referenced? Does my explanation make sense?

mattotodd avatar Apr 24 '13 19:04 mattotodd

I used (assuming obj is your document), isinstance(obj._data.get(field_name), DBRef) to check if the field has been dereferenced. If isinstance() returns True, is has not been dereferenced. If it has been dereferenced, you should get an instance of your model instead.

ruiwen avatar Sep 20 '13 13:09 ruiwen