-
-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi ForeignKey not working #47
Comments
Ok, so this is how you go from parent model to child model in # you need to go through QuerySet and select related model 'chargingpads' and select all()
# will get you a list of stations (and you created only one) so yours is at index 0
# note that you CAN use instance here (charging_station.objects...) but better be explicit as this can be changed in future releases
station = await ChargingStationOrm.objects.select_related('chargingpads').all()
assert len(station[0].chargingpads) == 4
# the same but get by pk (or any other filter combination that will return one row) so no list and no need for index
# note that if you do not provide filter parameters for now the query is limited to 2 rows - plan to change that in the future
# so if you provide just get() at the end there will be only 2 pads - it's a feature not a bug as empty get should not be issued with select_related
station2 = await ChargingStationOrm.objects.select_related('chargingpads').get(pk=1)
assert len(station2.chargingpads) == 4 This is actually the only not yet implemented load (you can load child from parent: so You can find some explanation and discussion in #27. |
Oh and if you want the other way around use: # only pads
pads = ChargingPad.object.all()
assert len(pads) == 4
# only pads and only pk field -> will complain if you try to exclude required fields
pads = ChargingPad.object.fields("id").all()
assert len(pads) == 4
# with station
pads = ChargingPad.object.select_related("charging_station").all()
assert len(pads) == 4 Note that So even in the first query above you can still go If you think that the documentation is not clear in this regard let me know. (Or you can issue a PR - it's always easier to start contributing with the docs). If that answers your question please close the issue :) |
Awesome. Thank you so much for the detailed how-to :) I feel encouraged to contribute. Let's see what happens ;) |
Thanks for the quick fix and the elaboration on the previous issue
I hope to contribute some code at some point.
I think I found one more bug:
Stacktrace:
The text was updated successfully, but these errors were encountered: