question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reverse foreign key lookups

See original GitHub issue

Now we have M2M, it makes sense to add something similar for reverse foreign key lookups.

This needs doing before v1.

Here’s an example API design:


class Manager(Table):
    name = Varchar()
    bands = ReverseLookup(LazyTableReference('Band', module_path=__module__))


class Band(Table):
    name = Varchar()
    manager = ForeignKey(Manager)


>>> await Manager.select(Manager.name, Manager.bands(Band.name, as_list=True))
[
    {
        "name": "Guido",
        "bands": ["Pythonistas"]
    }
]

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
dantownsendcommented, Dec 28, 2021

One option is for the user to do something like this, which would give better tab completion:

class Manager(Table):
   name = Varchar()
   bands: ReverseLookup


class Band(Table):
    name = Varchar()
    manager = ForeignKey(Manager, reverse_lookup='bands')
1reaction
sinisaoscommented, Aug 19, 2022

@dantownsend Thank you for your reply. No need to add this to the docs because if anyone needs a workaround, they can find it here. I’ve updated the gist with your suggestion so it’s all there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django reverse lookup of foreign keys - python - Stack Overflow
You can use events = venue.event_set to go the other way. Note that venue.event_set is a manager object, like Event.objects , so you...
Read more >
Reverse Lookups of Foreign Keys in Django - Delft Stack
Speaking of databases, Django simplifies most of the queries you can make while working with databases. One such query is the reverse look;...
Read more >
How to easily perform reverse look up in django
Recap. · 1. To make your models ready for django foreign key lookup. · 2. Add a related_name to your child model. ·...
Read more >
[magic-removal] Reverse foreign-key lookups should take ...
Currently reverse ForeignKey lookups are impossible for models which are related to other models more than once. Could this be fixed by using...
Read more >
Django backward relationship lookup - Agiliq
In such a scenario, we tend to use Student model. It's more intuitive because the Foreign Key relationship exists from Student to Group....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found