Closed yash2001cp closed 3 months ago
The issue has to do with Peewee putting the performed_by_id
on u.audit.performed_by_id
. Since there is no true foreign-key there, the introspection in model_to_dict()
is not finding it. The workaround is to do something like this:
# Add call to ".objects()" in order to put the performed_by_id directly onto the User obj.
query = (User
.select(User, Audit.performed_by_id,)
.join(Audit, on=(User.id == Audit.object_id))
.join(latest_query,
on=((Audit.object_id == latest_query.c.object_id) &
(Audit.created_at == latest_query.c.max_created_at)))
.objects())
# Specify the extra attr we want here:
u = query.first()
data = model_to_dict(u, extra_attrs=['performed_by_id'])
print(data)
The model_to_dict
is not a panacea and can only do so much. You're far better off just writing your own serialization for anything beyond the most trivial case.
getting key error; i am using prefetch so using model_to_dict kinda became mandatory (haven't added that here ) ; am i missing something ?? using model_to_dict(u, only = [User.id,Audit.performed_by_id]) doesn't work as well