You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you don't include this field in a select query and then try to access it, you get DoesNotExist error:
x=Model.select(Model.id).get() # related_id field not includedprint(x.related_id) # raises DoesNotExist
... which is not very expected, considering lazy_load=False.
The root of exception is in the ForeignKeyAccessor.get_rel_instance method, and I believe it should be changed like this:
def get_rel_instance(self, instance):
value = instance.__data__.get(self.name)
if value is not None or self.name in instance.__rel__:
if self.name not in instance.__rel__ and self.field.lazy_load:
obj = self.rel_model.get(self.field.rel_field == value)
instance.__rel__[self.name] = obj
return instance.__rel__.get(self.name, value)
- elif not self.field.null:+ elif not self.field.null and self.field.lazy_load:
raise self.rel_model.DoesNotExist
return value
The text was updated successfully, but these errors were encountered:
Actually scratch that - this is not a regression. There doesn't seem to be a reliable way of differentiating between a model instance you have explicitly chosen not to select the foreign-key value and a model instance that is missing a non-null foreign-key. Lazy-load really deals with converting a foreign-key value (e.g. integer id) into a full object.
That being said, I agree that the error is misplaced and this should just return None if lazy-load has been disabled. I've made the patch as you suggested.
I have a model with a foreign key field like this:
If you don't include this field in a select query and then try to access it, you get
DoesNotExist
error:... which is not very expected, considering
lazy_load=False
.The root of exception is in the
ForeignKeyAccessor.get_rel_instance
method, and I believe it should be changed like this:The text was updated successfully, but these errors were encountered: