I am getting this error AttributeError: 'GuildCluster' object has no attribute 'guilds'
when I try to get the guilds
attribute (which is a ReverseRelation fields) from a GuildCluster
object.
I already tried to check the relations, the foreign keys, to rename the attribute in case it was already used. I also tried to define the Guild
class before GuildCluster
, it returns the same error.
Here is the GuildCluster
model (a part of it):
class GuildCluster(Model):
class Meta:
table = "guild_clusters"
id = fields.IntField(pk=True)
name = fields.CharField(max_length=255)
infos: fields.ReverseRelation["ClusterInfo"]
guilds: fields.ReverseRelation["Guild"]
users: fields.ReverseRelation["User"]
And here is the Guild
model (a part of it too):
class Guild(Model):
class Meta:
table = "guilds"
id = fields.BigIntField(pk=True)
cluster = fields.ForeignKeyField("main.GuildCluster", related_name="cluster_guilds")
infos: fields.ReverseRelation["GuildInfo"]
You can note that the infos
and users
attribute are working fine.
The bug was caused by this line in Guild
: cluster = fields.ForeignKeyField("main.GuildCluster", related_name="cluster_guilds")
.
If you want to access to the related fields of a Model, you have to use the name that you set in the ForeignKeyField
.
So here, to make the previous code work you have to call <GuildCluster>.cluster_guilds
or change related_name="cluster_guilds"
to related_name="guilds"