Skip to Content
Menu
This question has been flagged
2 Replies
18309 Views

Hi. How can I use search_count? 
[controllers.py]

def register_session(self, prevouseURL, currentURL):

    remoteAddr = request.httprequest.environ['REMOTE_ADDR']

    _logger.error("currentURL : %r", currentURL)

    registerSession = SessionVisitor()

    url = URLList()

    _logger.error(url.search_count([('url', '=', currentURL)])>0)


[models.py]

class URLList(models.Model):

    _name = 'webvisitorcalc.url_list'

    url = fields.Char(string="URL", required=True)

    target_session_id = fields.One2many('webvisitorcalc.session_visitor', 'target_url_ids', string='Target URL')


In this situation i am receiving error:

2016-06-06 14:41:33,108 30086 ERROR odoov8 openerp.http: Exception during JSON request handling.

Traceback (most recent call last):

File "/home/skif/odoo/openerp/http.py", line 540, in _handle_exception

    return super(JsonRequest, self)._handle_exception(exception)

File "/home/skif/odoo/openerp/http.py", line 577, in dispatch

    result = self._call_function(**self.params)

File "/home/skif/odoo/openerp/http.py", line 313, in _call_function

    return checked_call(self.db, *args, **kwargs)

File "/home/skif/odoo/openerp/service/model.py", line 118, in wrapper

    return f(dbname, *args, **kwargs)

File "/home/skif/odoo/openerp/http.py", line 310, in checked_call

    return self.endpoint(*a, **kw)

File "/home/skif/odoo/openerp/http.py", line 806, in __call__

    return self.method(*args, **kw)

File "/home/skif/odoo/openerp/http.py", line 406, in response_wrap

    response = f(*args, **kw)

File "/home/skif/odoo/my-modules/webvisitorcalc/controllers.py", line 31, in register_session

    _logger.error(url.search_count([('url', '=', currentURL)])>0)

AttributeError: 'NoneType' object has no attribute 'search_count'


Ok. I modified code.

[controllers.py]

def register_session(self, prevouseURL, currentURL):

    remoteAddr = request.httprequest.environ['REMOTE_ADDR']

    _logger.error("currentURL : %r", currentURL)

    registerSession = SessionVisitor()

    url = URLList()

    _logger.error(url.search_count([('url', '=', currentURL)])>0)

    if url.url_exist():

        _logger.info("URL exist in DB ")

    else:

        _logger.info("URL NOT exist in DB ")



[models.py]

class URLList(models.Model):

    _name = 'webvisitorcalc.url_list'

    url = fields.Char(string="URL", required=True)

    target_session_id = fields.One2many('webvisitorcalc.session_visitor', 'target_url_ids', string='Target URL')


    @api.multi

    def url_exist(self, urlForCheck):

        _logger.error("Check URL exist in DB ")

        result = False

        if (self.search_count([('url', '=', urlForCheck)])>0):

            result = True

        return result


I'm receive error:

2016-06-06 14:56:50,797 30796 ERROR odoov8 openerp.http: Exception during JSON request handling.

Traceback (most recent call last):

File "/home/skif/odoo/openerp/http.py", line 540, in _handle_exception

    return super(JsonRequest, self)._handle_exception(exception)

File "/home/skif/odoo/openerp/http.py", line 577, in dispatch

    result = self._call_function(**self.params)

File "/home/skif/odoo/openerp/http.py", line 313, in _call_function

    return checked_call(self.db, *args, **kwargs)

File "/home/skif/odoo/openerp/service/model.py", line 118, in wrapper

    return f(dbname, *args, **kwargs)

File "/home/skif/odoo/openerp/http.py", line 310, in checked_call

    return self.endpoint(*a, **kw)

File "/home/skif/odoo/openerp/http.py", line 806, in __call__

    return self.method(*args, **kw)

File "/home/skif/odoo/openerp/http.py", line 406, in response_wrap

    response = f(*args, **kw)

File "/home/skif/odoo/my-modules/webvisitorcalc/controllers.py", line 33, in register_session

    if url.url_exist():

AttributeError: 'NoneType' object has no attribute 'url_exist'


In doc (https://www.odoo.com/documentation/8.0/howtos/backend.html) present:

[openacademy/models.py]

...

    @api.multi

        def copy(self, default=None):

        default = dict(default or {})

        copied_count = self.search_count([('name', '=like', u"Copy of {}%".format(self.name))])

...

In doc (http://odoo-new-api-guide-line.readthedocs.io/en/latest/environment.html?highlight=search#model) present:  

>>> self.search_count([('is_company', '=', True)])

26L

I can not understand what I forgot add in my code. Why do I receive errors?

Avatar
Discard
Best Answer

use below code:


self.pool['webvisitorcalc.url_list'].search_count([('url', '=', currentURL)])

Avatar
Discard
Best Answer

Hello,

in the last modified code, your error is not in the search_count, it is

"AttributeError: 'NoneType' object has no attribute 'url_exist'" 


so you need to check the url variable ...

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 15
2811
2
Mar 15
3439
0
Sep 23
1026
2
Jun 23
2471
1
Aug 22
10774