Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Using search count

By
Skif
on 6/6/16, 11:10 AM 438 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?

0

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


Ahmed M.Elmubarak
On 6/7/16, 2:40 AM

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 ...

0
Krupesh Laiya
On 6/7/16, 2:31 AM

use below code:


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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 6/6/16, 11:10 AM
Seen: 438 times
Last updated: 6/7/16, 2:40 AM