Open borsdenold opened 8 years ago
Additionaly:
def has_related_value(obj, field, channel_val):
if '__' not in field:
filter_by_val = channel_val
property_name = field
else:
property_name, filter_by_val = field.split('__', 1)
attr = getattr(obj, property_name)
if hasattr(attr, 'all'):
return getattr(obj, property_name).filter(**{filter_by_val: channel_val}).exists()
else:
filter_query = {'pk': obj.pk}
filter_query[field] = channel_val
return obj.__class__.objects.filter(**filter_query).exists()
Why there is condition if hasattr(attr, 'all'):
? In what events is it required? Perhaps, I am wrong, but I think, that
filter_query = {'pk': obj.pk}
filter_query[field] = channel_val
return obj.__class__.objects.filter(**filter_query).exists()
will be work fine with every filter_query.
I have two models: Board and User
and I made UserRouter, where watch users, who have at least one common board with current login user.
But there was one problem: it did not automatically update list of boards ids. So, i decided, if user id is permanently constant, then it will be better use it.
But there are another problem, which i found in channel_utils.py in function has_related_value. It checks condition in this line:
getattr(obj, property_name).filter(**{filter_by_val: channel_val}).exists()
. But there it is getting current user relationship objects. So, i changed this line toobj.__class__.objects.filter(pk=obj.pk, **{field: channel_val}).exists()
and it begin works correctly (at least, i think so). And i want to ask, is there any ways to make it without changing package code?