python - How can I retrieve data from multiple tables -
i need retrieve data multiple tables, dynamically built filter might or might not use data of tables.
so have this:
class solution(models.model): name = models.charfield(max_length=max, unique=true) # other data class exporttrackingrecord(models.model): tracked_id = models.integerfield() solution = models.foreignkey(solution) # other data then elsewhere need do:
def get_data(user_provided_criteria): etr = exporttrackingrecord.objects.filter(make_q_object(user_provided_criteria)).select_related() data in etr: s = data.solution # things data both tables as far can tell, if happen filter on field in solution, django join, , select_related both objects. if filter on fields in exporttrackingrecord there no join, , django generate new query each exporttrackingrecord in queryset (which thousands...)
i new django, there reasonable way force join?
select_related() key problem. if don't use , don't filter on fields of related model django not join , cause query every row in result if accessing data of related model.
if exporttrackingrecord.objects.filter(...).select_related('solution') force django join solution table.
if need same in other direction, through reverse foreign key relation ship need prefetch_related(), same many-to-many relations
Comments
Post a Comment