0

I am trying to select some data from the database.

I tried to call a function like this :

class Test(models.Model): 
    _name = 'test.test'
    _description = 'test.test'

    emri = fields.Char()
    mbiemri = fields.Char()
    qyteti = fields.Char()

    def getdata(self): 
        data = self.env['test3'].search([])
        print(data)

I am getting this error : AttributeError: 'function' object has no attribute 'env'

Kenly
  • 24,317
  • 7
  • 44
  • 60
Miga
  • 1
  • How did you call `getdata`? – Kenly Jun 04 '20 at 07:42
  • I am trying to call it in python console in PyCharm IDE. – Miga Jun 04 '20 at 07:47
  • Launch odoo as a python [console environment](https://www.odoo.com/documentation/13.0/reference/cmdline.html#shell) – Kenly Jun 04 '20 at 07:57
  • Yes in this way i have created my module , but when i write the function 'self' does not have the 'env' property neither the 'cr' property. – Miga Jun 04 '20 at 08:11
  • After starting the shell, use the following syntax to call `getdata` method: `self.env['test.test'].getdata()` – Kenly Jun 04 '20 at 14:27
  • It does not solve my problem , but thank you for your response anyway. – Miga Jun 04 '20 at 14:32
  • What is the problem exactly? – Kenly Jun 04 '20 at 19:40
  • I am trying to get some data from database and then to display those data in odoo view and I was trying with the method above. I want to call the function in order to see what data actually i was selecting. But when I call the function I am getting this error: AttributeError: 'function' object has no attribute 'env'. I don't know if I was clear. – Miga Jun 05 '20 at 08:35
  • The missing part is how you started odoo and how you called that method – Kenly Jun 05 '20 at 08:55
  • I have configured PyCharm for odoo development and there I called the method. – Miga Jun 05 '20 at 09:00
  • Describe how you configure and how you make the call – Kenly Jun 05 '20 at 09:13
  • I have follow this tutorial for configuration :https://youtu.be/E21VfQqX55c. And in PyCharm console I make the call in this way : Test.getdata(Test.getdata) – Miga Jun 05 '20 at 09:45
  • You called the `gedata` method as a class method and you passed `Test.getdata` reference (function reference) this is why you got `AttributeError: 'function' object has no attribute 'env'` . – Kenly Jun 05 '20 at 09:57
  • But shouldn't the method be inside of a class ? Or if you can tell me in short terms how can I display some data from database. Sorry for disturbing so much :) – Miga Jun 05 '20 at 10:44
  • No problem, `getdata` is an [instance method and you call it like a class method](https://stackoverflow.com/questions/17134653/difference-between-class-and-instance-methods). you can use Odoo shell to interact with [ORM](https://www.odoo.com/documentation/13.0/reference/orm.html#reference-orm) and its functionalities (you must add shell argument when starting Odoo). You can also add a button to run that method or use `pgadmin` to use SQL queries. If you need to add some functionalities to Odoo I will be pleased to help you. – Kenly Jun 05 '20 at 12:55

0 Answers0