octabytes / FireO

Google Cloud Firestore modern and simplest convenient ORM package in Python. FireO is specifically designed for the Google's Firestore
https://fireo.octabyte.io
Apache License 2.0
250 stars 29 forks source link

FireO Serializer #158

Closed AnyRob8 closed 2 years ago

AnyRob8 commented 2 years ago

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 ?

AxeemHaider commented 2 years ago

What you want to achieve?

You can convert model into dict to_dict() and dict into model Model.from_dict(d)

AnyRob8 commented 2 years ago

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 ?

AnyRob8 commented 2 years ago

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.