Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
26733 Zobrazení

Hi !

I'm new to Odoo 13 and Python so everything is a bit confusing at the moment.

My problem is that I need to do a request like "SELECT ... FROM ... WHERE ...", but I don't understand how I should do it here.

I have the first part which looks like that "self.env['my.table'].search([('the_this_i_want')])".

I would like to add something like "self.env['my.table'].search([('the_thing_i_want')]).where([('id','=',self.id)])".

Thanks.


Avatar
Zrušit
Nejlepší odpověď

What you put in search parameter (called domain) it's your where criteria.

You really should read the ORM documentation about domain search

The domain it's written in Polish Notation system, because you can normalize (stringify) the search string (useful from client-side) - more in this post.

For your example:

self.env['my.model'].search([('id', '=', self.id)])

The same (searching by id or a list of ids) can also be done using special browse method like this

self.env['my.model'].browse(self.id)
self.env['my.model'].browse([1, 2, 3])

You have to be careful because browse doesn't check if the record really exists in the DataBase, if you want to be sure it exists, you can use exists() method after it, like this:

self.env['my.model'].browse([1, 2, 3]).exists()

By default, if you write multiple conditions, they are treated as AND

self.env['my.model'].search([('my_field', '=', 'value'), ('my_other_field', '!=', False)])

To build more complex searches, you really have to check documentation, understand how to write complex domains using RPN (with ORs and ANDs) and look in Odoo code for more examples

Avatar
Zrušit

Great answer! I updated it to add a link to a post explaining Polish Notation as it applies to Odoo domains.

Autor Nejlepší odpověď

Hi, thanks a lot for your reply.

Ok so it gives me some ligth on my problem. So I put my criteria in "search()", but if I do that it returns me an id, but it's not the thing I want.

I should give you a more precise example.

I want a date stored in my DB, in a table called 'ticket'. But it want this date depends on a specifique id.

So I should do something like : "self.env['ticket'].search([('id','=',self.id)])"

But where do I say that I want the "date" and not the id ?

In SQL it would looks like : "SELECT date FROM ticket WHERE id = self.id.

Thanks a lot for your help.

Avatar
Zrušit

It returns a ticket object, not only the id. (But if you try to print it, you will only get the id printed)
ticket = self.env['ticket'].search([('id','=',self.id)])
ticket.name_of_field
eg: ticket.name, ticket.date, ticket.id

Autor

I didn't know that ! OMG tanks a lot, you save me precious time !

Related Posts Odpovědi Zobrazení Aktivita
2
zář 24
44
2
srp 24
1432
6
srp 18
19991
0
pro 23
17
1
dub 24
7368