I would like to know if it’s possible to have a ManyToMany relationship with an extra filed.
I have a drug model that may contains multiple molecules models.
I would like to display this relation with an additional field: quantity.
I need to display the quantity at the same level as the molecule's information.
This is my endpoint :
@router.get('/', response_model=List[DrugResponse])
async def drugs():
"""Returns a list of drugs. """
return await DrugResponse.from_queryset(Drug.all())
This is my models :
class Drug(Model):
id = fields.IntField(pk=True)
label = fields.CharField(max_length=128, null=False)
class Meta:
table = "drugs"
class DrugMolecule(Model):
drug = fields.ForeignKeyField("models.Drug", related_name='molecules', null=False)
molecule = fields.ForeignKeyField("models.Molecule", related_name=False, null=False)
quantity = fields.FloatField(null=True)
class Meta:
table = 'drug_molecule'
class Molecule(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=128, null=False)
class Meta:
table = 'molecules'
class PydanticMeta:
exclude = ["administration_context"]
I would like that my endpoint return an object structured like this :
Hello,
I would like to know if it’s possible to have a ManyToMany relationship with an extra filed. I have a drug model that may contains multiple molecules models. I would like to display this relation with an additional field: quantity. I need to display the quantity at the same level as the molecule's information.
This is my endpoint :
This is my models :
I would like that my endpoint return an object structured like this :
But for now, my endpoint return an object like this :
Could you please explain me how I can get the well structured return please ?