1

I am using odoo 14. I created a custom addon. in my addon there is a model below

class Merchant(models.Model):
_name = "ecommece_advance.merchant"

name        = fields.Char( required=True)
email       = fields.Char(required=True)
address     = fields.Char()
website     = fields.Char()
latitude    = fields.Float(digits=(3,6))
longitude   = fields.Float(digits=(3,6))
contact     = fields.Char(required=True)
alternative_contact = fields.Char()
market_id  = fields.Many2one('ecommece_advance.market',
    ondelete='cascade', string="Market", required=True)

I created a web controller for external use and return a list of merchant data. Odoo says my model is not serializable to return from the controller. I find no way to serialize my model. please help me out. thanks in advance.

below sample solution is not accepted --- *return data from the model by creating JSON *get data and create JSON in the controller

roconmachine
  • 1,876
  • 2
  • 17
  • 24

1 Answers1

0

I find a solution but not proper.

add a def in every model of the addon

    def getJson(self):
    data = {}

    for field in self._model_fields :
        if str(self[field]).startswith('ecommece_advance') :
            data[field] = self[field].getJson()
        elif isinstance(self[field], models.Model) :
            break
        elif isinstance(self[field], fields.datetime):
            data[field] = str(self[field])
        else:
            data[field] = self[field]
    return data

Now from the controller, you can call searchModel.getJson() to get serializable data. above method is a generic method within addon and one line of modification can work for other addons also.

I know it is not a proper way of solving problems. If you guys have any better solution please comment.

roconmachine
  • 1,876
  • 2
  • 17
  • 24