Skip to content

2 models need to relate to each other via different fields #960

Answered by alexol91
DiceNameIsMy asked this question in Q&A
Discussion options

You must be logged in to vote

In my opinion, this problem have two diferents ways to by resolved.

  1. Define the relationship names, so that Ormar understands that you are referring to two different relationships. That is, by saying that an item belongs to a basket, you create a relationship, but it must not conflict with the reverse of initial_item, which has a reverse link to the basket for which it is first, so the item variable (for all items) is called basket, but the related_name of initial_item is called “for_basket”

`class Basket(ormar.Model):
id: int = ormar.Integer(primary_key=True)
initial_item: Item = ormar.ForeignKey(Item, related_name="for_basket", nullable=True)

class Item(ormar.Model):
id: int = ormar.In…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@DiceNameIsMy
Comment options

Answer selected by DiceNameIsMy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants