Table res_groups_users_rel:

    uid | gid
-----------------
    4      3
    4      12
    4      9

Table res_groups:

    id | name           | comment
------------------------------------
     3   Employee         All the Employees 
     9   Contact          Creation of Contact
     12  Manager          This is MyModule

This are my tables. As you can see, the "uid" can contain several "gid", but I only want the "gid" who has in the comment "MyModule". I'm trying the following:

def _get_user_group(self, cr, uid, context=None):
actual_id = self.pool.get('res.users').browse(cr, uid, uid).id

query_A = 'SELECT gid FROM res_groups_users_rel WHERE uid = %s'
cr.execute(query_A, (actual_id,))
query_A_results = cr.fetchall()

if query_A_results:
    for gid in query_A_results:

        query_B = 'SELECT name, comment FROM res_groups WHERE id = %s'
        cr.execute(query_B, (gid[0],))
        query_B_results = cr.fetchall()

        if query_B_results:
            for names in query_B_results:
                s = names[1]
                if "MyModule" in s:
                    return names[0]

So, this function should return me "Manager", but it's giving me the error:

TypeError: argument of type 'NoneType' is not iterable

有帮助吗?

解决方案

You really shouldn't try to do this by getting all the results and iterating. It's a simple SQL query:

cursor.execute('SELECT g.name FROM g.res_groups '
               'JOIN res_groups_users_rel r ON r.gid = g.id '
               'WHERE r.uid = %s AND g.comment LIKE "%%%s%%"',
               (actual_id, "MyModule"))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top