Closed AnyRob8 closed 2 years ago
What you want to achieve?
You can convert model into dict to_dict()
and dict into model Model.from_dict(d)
Thank you for your response !
I am trying to get a dict of model with its ReferenceField
field, for example:
From these informations:
class User(Model):
name = fields.TextField()
last_name = fields.TextField()
email = fields.TextField()
class Product(Model):
name = fields.TextField()
count = fields.NumberField()
price = fields.NumberField()
owner = fields.ReferenceField(User)
I would like to get:
{
name : "watch"
count: 10
price: 10
owner: {
name : "Any"
last_name : "Rob"
email : "example@gmail.com"
}
}
Do you have any idea how can I do that ?
I found a solution to my problem !
With this configuration
class User(Model):
name = fields.TextField()
last_name = fields.TextField()
email = fields.TextField()
class Product(Model):
name = fields.TextField()
count = fields.NumberField()
price = fields.NumberField()
owner = fields.ReferenceField(User)
def to_dict(self):
model_dict = super(Product, self).to_dict()
owner_dict= self.owner.to_dict()
model["owner "] = owner_dict
return model_dict
The call of to_dict()
is enough.
I am trying to serialize data with rest_framework serializer and it's not working due to an AttributeError: AttributeError: 'Meta' object has no attribute 'concrete_model'
Is there a tool to serialize FireO Model ?