Fetching the complete 'date' field was not coming form 'hr.employee' model - odoo

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

Related

How to add sql constrain of date only from field datetime odoo 10?

How to add sql constrain of date only from field datetime
date = fields.Datetime('Date', store=True, default=fields.Datetime.now,
)
In this case, you don't need sql restrictions. A better option is to extend the field and change its data type to Date:
class NewClass(models.Model):
_inherit = "original.class"
...
date = fields.Date('Date', store=True, default=fields.Date.today())
If you don't want to do this, use instead the constraints decorator over your field:
#api.constrains('date')
def check_data_type(self):
if not condition:
raise ValidationError("Error!")
In any case, if you still want to add a sql constraints you can use this in your model:
class YourClass(models.Model):
_name = "your.class"
...
_sql_constraints = [('date', 'CHECK (your check)', 'Data no pass the check')]
I hope this helps!

how to get current record id using onchange for one2many in odoo 10?

I have tried below code for onchange function but return some unreadable code
class sales_targets(models.Model):
_name = 'sale.target'
category = fields.Selection([
('normal', 'Product'), ('cloud', 'Cloud EYE'), ('tech', 'Technical Support Group'), ('db', 'Database'),
('odoo', 'Odoo'), ('can', 'CAN'), ('tod', 'TOD'), ('rental', 'Rental'), ('tec', 'TEC'), ('top', 'TOP'),
('tor', 'TOR'), ('tos', 'TOS'),
('aws', 'AWS')], 'Category', default='normal',
help="A category of the view type is a virtual category that can be used as the parent of another category to create a hierarchical structure.")
team_ids=fields.Many2many('crm.team','team_target_rel','target_ids','team_id','Sales Team')
from_date=fields.Date('Budget From Date')
to_date = fields.Date('Budget To Date')
no_call=fields.Float('No of Calls')
target_line=fields.One2many('target.line','target_id','Target Line', copy=True)
#api.multi
#api.onchange('team_ids')
def _onchange_tem_salesperson(self):
print ".....",self.id,self._origin
above code onchange function result is
...... <odoo.models.NewId object at 0x7f72aff2b290> sale.target()
In onchange event odoo create a dummy record for you.
In order to get the id, odoo passes the record in origin property.
self._origin.id. # keep in mind in create id is instance of NewId object Not integer
I'm not sure if it's self. _origin or self.origin but i'm sure that it's in origin because i used it before. Hope it helped you
thanks for you advice and then finally got solution for this page How to pass value with onchange one2many variable in odoo? please up vote my question if my question is worth

Setting group_by in specialized query

I need to perform data smoothing using averaging, with a non-standard group_by variable that is created on-the-fly. My model consists of two tables:
class WthrStn(models.Model):
name=models.CharField(max_length=64, error_messages=MOD_ERR_MSGS)
owner_email=models.EmailField('Contact email')
location_city=models.CharField(max_length=32, blank=True)
location_state=models.CharField(max_length=32, blank=True)
...
class WthrData(models.Model):
stn=models.ForeignKey(WthrStn)
date=models.DateField()
time=models.TimeField()
temptr_out=models.DecimalField(max_digits=5, decimal_places=2)
temptr_in=models.DecimalField(max_digits=5, decimal_places=2)
class Meta:
ordering = ['-date','-time']
unique_together = (("date", "time", "stn"),)
The data in WthrData table are entered from an xml file in variable time increments, currently 15 or 30 minutes, but that could vary and change over time. There are >20000 records in that table. I want to provide an option to display the data smoothed to variable time units, e.g. 30 minutes, 1, 2 or N hours (60, 120, 180, etc minutes)
I am using SQLIte3 as the DB engine. I tested the following sql, which proved quite adequate to perform the smoothing in 'bins' of N-minutes duration:
select id, date, time, 24*60*julianday(datetime(date || time))/N jsec, avg(temptr_out)
as temptr_out, avg(temptr_in) as temptr_in, avg(barom_mmhg) as barom_mmhg,
avg(wind_mph) as wind_mph, avg(wind_dir) as wind_dir, avg(humid_pct) as humid_pct,
avg(rain_in) as rain_in, avg(rain_rate) as rain_rate,
datetime(avg(julianday(datetime(date || time)))) as avg_date from wthr_wthrdata where
stn_id=19 group by round(jsec,0) order by stn_id,date,time;
Note I create an output variable 'jsec' using the SQLite3 function 'julianday', which returns number of days in the integer part and fraction of day in the decimal part. So, multiplying by 24*60 gives me number of minutes. Dividing by N-minute resolution gives me a nice 'group by' variable, compensating for varying time increments of the raw data.
How can I implement this in Django? I have tried the objects.raw(), but that returns a RawQuerySet, not a QuerySet to the view, so I get error messages from the html template:
</p>
Number of data entries: {{ valid_form|length }}
</p>
I have tried using a standard Query, with code like this:
wthrdta=WthrData.objects.all()
wthrdta.extra(select={'jsec':'24*60*julianday(datetime(date || time))/{}'.format(n)})
wthrdta.extra(select = {'temptr_out':'avg(temptr_out)',
'temptr_in':'avg(temptr_in)',
'barom_mmhg':'avg(barom_mmhg)',
'wind_mph':'avg(wind_mph)',
'wind_dir':'avg(wind_dir)',
'humid_pct':'avg(humid_pct)',
'rain_in':'avg(rain_in)',
'rain_sum_in':'sum(rain_in)',
'rain_rate':'avg(rain_rate)',
'avg_date':'datetime(avg(julianday(datetime(date || time))))'})
Note that here I use the sql-avg functions instead of using the django aggregate() or annotate(). This seems to generate correct sql code, but I cant seem to get the group_by set properly to my jsec data that is created at the top.
Any suggestions for how to approach this? All I really need is to have the QuerySet.raw() method return a QuerySet, or something that can be converted to a QuerySet instead of RawQuerySet. I can not find an easy way to do that.
The answer to this turns out to be really simple, using a hint I found from
[https://gist.github.com/carymrobbins/8477219][1]
though I modified his code slightly. To return a QuerySet from a RawQuerySet, all I did was add to my models.py file, right above the WthrData class definition:
class MyManager(models.Manager):
def raw_as_qs(self, raw_query, params=()):
"""Execute a raw query and return a QuerySet. The first column in the
result set must be the id field for the model.
:type raw_query: str | unicode
:type params: tuple[T] | dict[str | unicode, T]
:rtype: django.db.models.query.QuerySet
"""
cursor = connection.cursor()
try:
cursor.execute(raw_query, params)
return self.filter(id__in=(x[0] for x in cursor))
finally:
cursor.close()
Then in my class definition for WthrData:
class WthrData(models.Model):
objects=MyManager()
......
and later in the WthrData class:
def get_smoothWthrData(stn_id,n):
sqlcode='select id, date, time, 24*60*julianday(datetime(date || time))/%s jsec, avg(temptr_out) as temptr_out, avg(temptr_in) as temptr_in, avg(barom_mmhg) as barom_mmhg, avg(wind_mph) as wind_mph, avg(wind_dir) as wind_dir, avg(humid_pct) as humid_pct, avg(rain_in) as rain_in, avg(rain_rate) as rain_rate, datetime(avg(julianday(datetime(date || time)))) as avg_date from wthr_wthrdata where stn_id=%s group by round(jsec,0) order by stn_id,date,time;'
return WthrData.objects.raw_as_qs(sqlcode,[n,stn_id]);
This allows me to grab results from the highly populated WthrData table smoothed over time increments, and the results come back as a QuerySet instead of RawQuerySet

SQL select distinct value

I'm trying to select the following data with the limited information. The problem is that when I have added the .select distinct section it has killed my query.
#activities = Availability.select.("DISTINCT user_id").where("team_id = ? and schedule_id = ?", current_user[:team_id], #next_game).last(5)
There's one too many dot's in there as the 'DISTINCT user_id' is the arguments for the select method call.
So:
Availability.select("DISTINCT user_id").where("team_id = ? and schedule_id = ?", current_user[:team_id], #next_game).last(5)
Also be aware that you're now only selecting one attribute and you'll get a partial representation of the classes back. To circumvent this just select the attributes you need later in the code.
Availability.select("DISTINCT(`user_id`), `team_id`").where("team_id = ? and schedule_id = ?", current_user[:team_id], #next_game).last(5)
etc.
Hope this helps.

How to get the Database Id from an XML Id

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]