Open pyahmed opened 10 months ago
You Preset model doesn't have preset field. So you can't load this relation
I was following the Getting started examples from here: https://tortoise.github.io/getting_started.html
Where .prefetch_related()
is used in the same way, not sure why but I thought .fetch_related()
would work the same way. Apprently not, but it does work with .fetch_related('setting1')
.
Getting started:
class Event(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=255)
# References to other models are defined in format
# "{app_name}.{model_name}" - where {app_name} is defined in tortoise config
tournament = fields.ForeignKeyField('models.Tournament', related_name='events')
participants = fields.ManyToManyField('models.Team', related_name='events', through='event_team')
def __str__(self):
return self.name
selected_events = await Event.filter(
participants=participants[0].id
).prefetch_related('participants', 'tournament')
The Event has participants and tournament fields. So It works. You model doesn't have the field you're trying to use, so it doesn't work.
Describe the bug I've setup a ForeignKey field and appropriate model. When fetching the object I can't access the object under the ForeignKey field. Using .fetch_related says relation not found.
To Reproduce
So when I run
setup_data_and_test.py
I get:Expected behavior I would expect to access the data of the FK-object. Or am I missing something here? How could I access objects under ForeignKey-fields?
Additional context tortoise-orm: 0.20.0 OS: Win 10/x64 Py: Python 3.10.6