Skip to Content
Menu
This question has been flagged
4 Replies
2030 Views

Hi everyone,

I'm developping a small module for Odoo 8, and I need a field to be filled with current date.
I've been trying to do this through my python file but it raises this error "Expected singleton: test.test()"

On the forum I found this( https://www.odoo.com/fr_FR/forum/aide-1/question/expected-singleton-71132 ) topic but I couldnt get it. Below is my code. Thank you in advance for any help.

import time
class Test(models.Model):
    _name = 'test.test'   
    @api.one
    def a_fun(self):
        self.date=time.strftime("%d/%m/%Y")       
    date = fields.Date(default=a_fun)

Avatar
Discard
Author

To solve my problem I used date = fields.Date(default=fields.datetime.now() , readonly=True),

but if someone can give me a few explanation about the error I raised and the use of decorators in Odoo, I'll be very glad ! (I read the documentation, but I really dont get it, I'm quite new to Python programming).

Best Answer

Hi Belatrous

The difference is that the argument 'default' needs a value or a function that return a value to use it as default value for the field, fields.datetime.now() returns a value, and in your first attempt your a_fun method doesn't return a value, you could make it work like this:

@api.model
def a_fun(self):
return time.strftime("%d/%m/%Y")

Hope this help you

Avatar
Discard
Author

I'm still stuck on this issue, I've tried using your solution and Alex's too, it works if I have only one record but if there are more; I've again this issue. I've tried with@api.model and @api.multi. Below my code. Any idea ?

class Test(models.Model):

_name = 'test.test'

computed_date = fields.Datetime(compute='compute_date')

@api.multi

def compute_date(self):

self.computed_date=fields.datetime.now()

that is because you are mixin things, default argument is not the same as compute argument, for the compute argument you could do it like this:

@api.multi

def compute_date(self):

for elem in self:

elem.computed_date=fields.datetime.now()

but with that you will not get default values, just values in already created records

Best Answer

There is a difference between default and compute ,you can use api.one and api.multi in case of compute but no in case of default ,in case of default use api.model

Avatar
Discard
Author

I'm new to Odoo, so I dont understand what "compute" means. I've seen it in api documentation but I dont know what it is , can you help me please ?