I have create new module and create model as;
class CustomResCompany(models.Model):
_inherit = "res.company"
#api.model
def create(self, vals):
new_company = super(CustomResCompany, self).create(vals)
self.env['stock.picking.type'].sudo().create({
'name': 'Return',
'sequence_code': 'IN',
'code': 'outgoing',
'company_id': new_company.id,
})
return new_company
I install the module but when run, pointer not comes in this code. Means this code not work and no error comes.
But when i paste this code an other module model file, it works perfectly.
Please guide where i am missing some thing.
Related
I want to extend the "state" column of the mrp.production model. There was an example I found in https://www.odoo.com/de_DE/forum/how-to/developers-13/how-to-extend-fields-selection-options-without-overwriting-them-21529 but it seems to not work in odoo 11.
i.e. the __init__ signature changed to __init__(self, pool, cr) (guessing this from the error trace that I saw referencing model.__init__(pool, cr))
from odoo import models, fields
import logging
_logger = logging.getLogger(__name__)
class mrp_production(models.Model):
_inherit = 'mrp.production'
def __init__(self, pool, cr):
super(mrp_production, self).__init__(pool, cr)
states = self._columns['state'].Selection
_logger.debug('extend state of mrp.production')
state_other = ('other_state', 'My State')
if state_other not in states:
states.append(state_other)
The Error I'm receiving is:
AttributeError: 'mrp.production' object has no attribute '_columns'
You don't need to extend the __init__. Please look into the documentation to get a first look how to extend odoo business models.
For your example the correct code:
from odoo import models, fields
class MrpProduction(models.Model):
_inherit = 'mrp.production'
state = fields.Selection(
selection_add=[('other_state', 'My State')])
The tutorial you follow is for old API.
You can try
https://www.odoo.yenthevg.com/extend-selection-odoo-10/
from odoo import models, fields, api, _
class HrEmployee(models.Model):
_inherit = 'hr.employee'
gender = fields.Selection(selection_add=[('transgender', 'Transgender')])
I have a custom module called admission form with some fields suppose name, phone, email, etc. how to add this form to website module using templatr to work like contact form in contact us page when filled data is automatically created in new leads. instead of leads i want it to transfer the information to my custom module.
Summary: instruction to relate website to custom module.
class AdmissionForm(models.Model):
_name = 'admission.form'
name = fields.Char()
phone = fields.Integer()
email = fields.Char()
faculty = field.Many2one('res.faculty')
In ODOO Whenever you want to performe some task at the time of creation ,then you must override create method in your model (:admission.form).
Let say you want to create a partner just after creation of the record in admission.form model then follow these steps:
Override create method .
Call the super with the argument and hold it value in result.
Now do your task .
return result.
Code snippet:
#api.model
def create(self, vals):
result = super(AdmissionForm, self).create(vals)
new_vals = dict(name=result.name,
phone=result.phone,
email=result.email,
is_company=1,
supplier=1,
customer=1,
)
self.env['res.partner'].create(new_vals)
return result
In case if you want to do some task before creation of record then follow these steps:
Override create method .
Do your task .
Call the super with the argument and return it.
#api.model
def create(self, vals):
new_vals = dict(name=vals.get('name'),
phone=vals.get('phone'),
email=vals.get('email'),
is_company=1,
supplier=1,
customer=1,
)
partner=self.env['res.partner'].create(new_vals)
return super(AdmissionForm, self).create(vals)
I was writing create method for my own custom module.
def create(self, cr, uid,ids, context=None):
self.message_post(cr, uid, ids, body=_("Form Page created"), context=None)
but i am getting the following error when saving
AssertionError: Invalid thread_id; should be 0, False, an ID or a list with one ID
or sometimes
TypeError: create() got multiple values for keyword argument 'context'
i just want to post a message when it is created
Openerp 7 Create Method
def create(self, cr, uid, vals, context=None):
new_id = super(CRM_Lead, self).create(cr, uid, vals, context=context)
return new_id
odoo 8 Create Method:
class ClassName(models.Model):
_inherit = "model.name"
#api.model
def create(self, vals):
rec = super(ClassName, self).create(vals)
# ...
return rec
Make sure you use the exact name of the python class in the super function and also that you return the same object you get from it.
Track Visibility
You can set a mail.message.subtype that depends on an other to act through a relation field.
For better understanding track visibilty Refer This Link
There is two type fileds of track visibility
track_visibility='always'
track_visibility='onchange'
How can we pass context value to qweb report so that i can control the visibility of tables. I have a qweb report with lot of tables. Depending on the selection list, i want to control the view of these tables in qweb report. So my option was to control using context. But didn't find any way to pass the context. If there is any other opinion, please share.
Create parser class first
import time
from openerp.osv import osv
from openerp.report import report_sxw
class sale_quotation_report(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
super(sale_quotation_report, self).__init__(cr, uid, name, context=context)
self.localcontext.update({
‘key’: value,
‘function_name’: self.function_name,
})
def function_name(self):
### add some code if required..
Then define another class
class report_saleorderqweb(osv.AbstractModel):
_name = ‘module_name.report_sale_order_qweb’
_inherit = ‘report.abstract_report’
_template = ‘module_name.report_sale_order_qweb’
_wrapped_report_class = sale_quotation_report
Then you can call the localcontext method in that way
<span t-esc=”function_name(parameter)”/>
Refer our blog on Qweb report
Your question is not very clear on what exactly you want. For instance, I dont know what you mean by "Depending on the selection list", so I assume you have a wizard that prompts the user to select some options. If that is the case, you can pass the selection variable inside the data dictionary in the return statement of your print function.
def print_report(self, cr, uid, ids, context=None):
if context is None:
context = {}
datas = {'ids': context.get('active_ids', [])}
res = self.read(cr, uid, ids, ['date_start', 'date_end', 'user_ids'], context=context)
res = res and res[0] or {}
datas['form'] = res
if res.get('id',False):
datas['ids']=[res['id']]
return self.pool['report'].get_action(cr, uid, [], 'point_of_sale.report_detailsofsales', data=datas, context=context)
This passes the user selection under data['form']. You can then access the selections in qweb as data['form']['date_start']
I would like to enhance the class pathlib.Path but the simple example above dose not work.
from pathlib import Path
class PPath(Path):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
test = PPath("dir", "test.txt")
Here is the error message I have.
Traceback (most recent call last):
File "/Users/projetmbc/test.py", line 14, in <module>
test = PPath("dir", "test.txt")
File "/anaconda/lib/python3.4/pathlib.py", line 907, in __new__
self = cls._from_parts(args, init=False)
File "/anaconda/lib/python3.4/pathlib.py", line 589, in _from_parts
drv, root, parts = self._parse_args(args)
File "/anaconda/lib/python3.4/pathlib.py", line 582, in _parse_args
return cls._flavour.parse_parts(parts)
AttributeError: type object 'PPath' has no attribute '_flavour'
What I am doing wrong ?
You can subclass the concrete implementation, so this works:
class Path(type(pathlib.Path())):
Here's what I did with this:
import pathlib
class Path(type(pathlib.Path())):
def open(self, mode='r', buffering=-1, encoding=None, errors=None, newline=None):
if encoding is None and 'b' not in mode:
encoding = 'utf-8'
return super().open(mode, buffering, encoding, errors, newline)
Path('/tmp/a.txt').write_text("я")
Here is the definition of the Path class. It does something rather clever. Rather than directly returning an instance of Path from its __new__(), it returns an instance of a subclass, but only if it's been invoked directly as Path() (and not as a subclass).
Otherwise, it expects to have been invoked via either WindowsPath() or PosixPath(), which both provide a _flavour class attribute via multiple inheritance. You must also provide this attribute when subclassing. You'll probably need to instantiate and/or subclass the _Flavour class to do this. This is not a supported part of the API, so your code might break in a future version of Python.
TL;DR: This idea is fraught with peril, and I fear that my answers to your questions will be interpreted as approval rather than reluctant assistance.
You may be able to simplify your life depending on why you want to extend Path (or PosixPath, or WindowsPath). In my case, I wanted to implement a File class that had all the methods of Path, and a few others. However, I didn't actually care if isinstance(File(), Path).
Delegation works beautifully:
class File:
def __init__(self, path):
self.path = pathlib.Path(path)
...
def __getattr__(self, attr):
return getattr(self.path, attr)
def foobar(self):
...
Now, if file = File('/a/b/c'), I can use the entire Path interface on file, and also do file.foobar().
Combining some of the previous answers you could also just write:
class MyPath(pathlib.Path):
_flavour = type(pathlib.Path())._flavour
I have been struggling with this too.
Here is what i did, studying from the pathlib module.
Seems to me that is the cleaner way to do it, but if the pathlib module changes its implementation, it probably won't hold.
from pathlib import Path
import os
import pathlib
class PPath(Path):
_flavour = pathlib._windows_flavour if os.name == 'nt' else pathlib._posix_flavour
def __new__(cls, *args):
return super(PPath, cls).__new__(cls, *args)
def __init__(self, *args):
super().__init__() #Path.__init__ does not take any arg (all is done in new)
self._some_instance_ppath_value = self.exists() #Path method
def some_ppath_method(self, *args):
pass
test = PPath("dir", "test.txt")
Note
I have opened a bug track here after a little discussion on the Python dev. list.
A temporary solution
Sorry for this double answer but here is a way to achieve what I want. Thanks to Kevin that points me to the source of pathlib and the fact we have here constructors.
import pathlib
import os
def _extramethod(cls, n):
print("=== "*n)
class PathPlus(pathlib.Path):
def __new__(cls, *args):
if cls is PathPlus:
cls = pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath
setattr(cls, "extramethod", _extramethod)
return cls._from_parts(args)
test = PathPlus("C:", "Users", "projetmbc", "onefile.ext")
print("File ?", test.is_file())
print("Dir ?", test.is_dir())
print("New name:", test.with_name("new.name"))
print("Drive ?", test.drive)
test.extramethod(4)
This prints the following lines.
File ? False
Dir ? False
New name: C:/Users/projetmbc/new.name
Drive ?
=== === === ===
In order to inherit from pathlib.Path, you need to specify which OS, or "flavour" you're representing. All you need to do is specify that you are using either Windows or Unix (seems to be Unix based on your traceback) by inheriting from pathlib.PosixPath or pathlib.WindowsPath.
import pathlib
class PPath(pathlib.PosixPath):
pass
test = PPath("dir", "test.txt")
print(test)
Which outputs:
dir\test.txt
Using type(pathlib.Path()) as proposed in this answer does the exact same thing as directly inheriting from pathlib.PosixPath or pathlib.WindowsPath since instantiating pathlib.Path "creates either a PosixPath or a WindowsPath" (pathlib documentation).
If you know your application will not be cross-platform, it is simpler to directly inherit from the flavor Path that represents your OS.
Here is a simple way to do things regarding to the observation made by Kevin.
class PPath():
def __init__(self, *args, **kwargs):
self.path = Path(*args, **kwargs)
Then I will need to use a trick so as to automatically bind all the Path's methods to my PPpath class. I think that will be funny to do.
It's work too.
from pathlib import Path
class SystemConfigPath(type(Path())):
def __new__(cls, **kwargs):
path = cls._std_etc()
return super().__new__(cls, path, **kwargs)
#staticmethod
def _std_etc():
return '/etc'
name = SystemConfigPath()
name = name / 'apt'
print(name)
Printed:
/etc/apt
#staticmethod can be replaced by #classmethod