The osv.osv provides a get_xml_id method to find the XML Id for the provided Database Id.
What is the best way to do the opposite?
Knowing the XML Id (it was defined in the data loading file), how can I get the corresponding Database Id, so that I can refer to it in tour Python code?
The ir.model.data model also has a get_object() method returning a browsable record given a model name and an xml_id.
So, another solution could be:
m = self.pool.get('ir.model.data')
id = m.get_object(cr, uid, 'base', 'user_root').id
The ir_model_data object has a _get_id() method that does what you're looking for. You can see it in use in the res_users._get_admin_id() method:
def _get_admin_id(self, cr):
if self.__admin_ids.get(cr.dbname) is None:
ir_model_data_obj = self.pool.get('ir.model.data')
mdid = ir_model_data_obj._get_id(cr, 1, 'base', 'user_root')
self.__admin_ids[cr.dbname] = ir_model_data_obj.read(cr, 1, [mdid], ['res_id'])[0]['res_id']
return self.__admin_ids[cr.dbname]
Related
I am trying to grab a couple values from an sqlite database, but instead of returning the value I am SELECT-ing, it is returning that value in an array inside of another array.
My code:
def self.find(id, database_connection)
name = database_connection.execute("SELECT name FROM pokemon WHERE id = ?", id)
type = database_connection.execute("SELECT type FROM pokemon WHERE id = ?", id)
pokemon_inst = Pokemon.new(id: id, name: name, type: type, db: database_connection)
end
The Problem:
When I run pry.binding
name outputs [["Pikachu"]]
type outputs [["electric]]
Is this working correctly? I can't imagine I should I just be calling name[0][0] to access the data, right?
The execute() method wraps everything it is returning in an array, and each database row is placed in an array. So, for the single value being queried to be in two arrays is correct.
Since everything is working as it should I will be adjusting my code to this:
def self.find(id, database_connection)
pokemon = database_connection.execute("SELECT * FROM pokemon WHERE id = ?", id).flatten
name = pokemon[1]
type = pokemon[2]
pokemon_inst = Pokemon.new(id: id, name: name, type: type, db: database_connection)
end
The adjustments are to flatten the array when it is assigned the queried data and to assign the individual values from that array.
I am trying to get the data from 'date' field that 'date' field is in 'hr.employee' to 'hr.payslip'.I create a function for that task.
Code:
class employee_datecheck(models.Model):
_inherit = 'hr.payslip'
#api.onchange('employee_id')
#api.constrains('employee_id')
def date_check(self):
if self.employee_id:
self.t2 = self.date_from
self.tax
product_obj = self.env['hr.employee'].search([('name' , '=' ,self.employee_id.name)])
if product_obj:
for products in product_obj:
product_new_obj = self.env['hr.employee'].browse([products.id])
for tax in product_new_obj.joindate:
raise Warning(self.tax)
problem is:
The date was not fetching properly i.e it just showing the '2' instead of '2017-09-21'.Please help me.
I think you could try to print directly the joindate without doing a loop:
for products in product_obj:
product_new_obj = self.env['hr.employee'].browse([products.id])
self.tax = product_new_obj.joindate:
raise Warning(self.tax)
I hope this help you.
phani,
1) You code does not look from v8 as tagged on the question, code is v9+ new API.
2) Not sure why are you searching on Model product and employee because hr.playslp Modle itself has employee field employee_id that you can use to get the date. Also, you should not use onchange and constrains together
Below is sample code:
class employee_datecheck(models.Model):
_inherit = 'hr.payslip'
#api.onchange('employee_id')
def date_check(self):
if self.employee_id:
raise Warning(self.employee_id.joindate)
It will be good if you can give more detail for an accurate answer.
Bests
I need to set the value of sequence for a IrSequence model instance inside a Python method.
I would have as input values:
the ID of the IrSequence (and getting the ID of the IrSequenceDateRange if dates are used is also possible).
the value for the next value to be used in the sequence.
Given that ID and that value, how can I set up the next value programmatically -i.e. by python source code- for that sequence?
Thanks,
There are two methods to get the next value in a sequence:
1) Given the id:
next_seq = seq_record.next_by_id(cr, uid, seq_id, context)
2) Given the code:
next_seq = seq_record.next_by_code(cr, uid, seq_code, context=context)
But if you want to change the database value directly you can try to write the record:
seq_rec = self.env[ir_sequence].browse(seq_id)
seq_rec.write({'number_next': your_next_sequence})
I hope this helps
I'm wanting to write SQL for a Django field that uses CharField(choices=()) and have the display value show up in the SQL rather than the call value. Any idea how to do this? It's similar to get_FOO_display().
For reference's sake, here's my model:
class Person(models.Model):
STUDENT_CHOICES=(
(0,'None'),
(1,'UA Current LDP'),
(2,'UA LDP Alumni'),
(3,'MSU Current LDP'),
(4,'MSU LDP Alumni')
)
...
studentStatus=models.IntegerField(choices=STUDENT_CHOICES, verbose_name="Student Status", null=True, blank=True)
And my query:
def mailingListQuery(request):
...
if request.POST:
...
sql = """
...
per."studentStatus" # Here's where I want to access the display value
left outer join person as per on (per.party_id = p.id)
"""
Thanks in advance!
You can use something like:
STUDENT_CHOICES=(
('None', 'None'),
)
Also, avoid using raw SQL. If you really need it - always use parametrized queries
connection.cursor().execute('sql with %s params', [params])
I have the following Groovy code to return the max value from the 'id' column from the 'topic' table:
def rs = sql.executeQuery("select max(id) from topic")
def maxId = rs.getLong(1)
It doesn't work, I get the following error:
java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:209)...
Does any one know what the correct code is?
Thanks.
I think it would be easier if you'd use the method firstRow. You can either get the value from the result object by name or by index.
by name:
def row = sql.firstRow("select max(id) as max from topic")
def maxId = row.max
by index:
def row = sql.firstRow("select max(id) from topic")
def maxId = row[0]
Nobody seems to have mentioned that the index in rs.getLong(1) is out of bounds. Getting fields uses a starting index of 0. Binding fields uses a starting index of 1. Why? Historical SQL behaviour.