django-easy-audit
django-easy-audit copied to clipboard
Performance issue. FK fields are being queried to check to see if data changed
Thanks for the package. I have been using it and noticed looking into the PostgreSQL logs that a lot of queries are being performed on updates.
If a model with a FK is saved, any foreign key field with data is querying the FK table to see if data has changed. This happens two times (one for old and new). These maybe unnecessary DB hits.
I looked at the following and it seems that when get_field_value() is called the DB is hit to get the FK models str() value when all that is needed is the fk_field's id.
Am I missing something important?
Thanks
def model_delta(old_model, new_model):
delta = {}
fields = new_model._meta.fields
for field in fields:
old_value = get_field_value(old_model, field)
new_value = get_field_value(new_model, field)
if old_value != new_value:
delta[field.name] = [smart_text(old_value),
smart_text(new_value)]
def get_field_value(obj, field):
...
value = smart_text(getattr(obj, field.name, None))
...
You may be right. I haven't audited that code path. If that's the case, can you write up a PR which adjusts the code to check if the field is a FK, and does the local/in-memory resolution instead?