How to hide some rows of an One2many field in Odoo 10? - python

May be I am a bit distracted with the current situation, but I cannot understand why this is not working:
I have a model, named my.comodel. This model has a field named invisible, a Boolean.
On the other hand, I have another model, which has an One2many field named my_o2m_field and points to my.comodel. I just want that this field shows only the records whose field invisible is False:
my_o2m_field = fields.One2many(
comodel_name='my.comodel',
inverse_name='my_m2o_field',
domain="[('invisible', '=', False)]",
# domain=[('invisible', '=', False)],
string='Test',
)
I have tried with both domains (and a lot more) with no result. The point is when I go to the technical interface and look for the field in the database structure, the field is updated with all the changes I did to it, except for the domain, which always remains empty. And if I try to modify it through the interface just to test, I get the message which warns you to change field properties by code, not by interface.
Any ideas of what is happening? Or how can I manage what I want? I have an idea but it would be too messy.

Related

Odoo Pipeline report include a field from related model

In Odoo Opportunity Report I would like to add field customer from res.partner.
I created addon (which installed, and does other things besides, so I am sure that addon works) in which I have inherited from https://github.com/odoo/odoo/blob/10.0/addons/crm/report/crm_opportunity_report.py .
And added a field
customer = fields.Boolean('Customer', related='partner_id.customer', readonly=True)
But field Customer doesn't appear in report when I click '+' in Reports->Pipeline.
What did I miss?
It is not enough to define a field. Odoo reporting is working on database views. So by adding a new field, you have to change the view, too. Normally or in newer versions Odoo has good extendable view definitions by using the init(). In your example it's the old "bad to extend" view definition, so you have to override the whole init.

What is the right way to add a field in the middle in django

I am new to Django.
I was trying to design a model in django. First I did with adding some fields, then I migrated the code. Later I found that I should have some other fields. I added some new fields, let say a CharField. Then while I was doing the migration, its showing error like you are trying to add a non-nullable field without a default. Can anybody tell should I add every time a default value to a new field OR Is there any other way to handle this?
You would need to add null=True to the field parameter that gives you this error in your model to allow for null values or give it a default value default=<value> and then re-run your migration

How to perform lookup in form view - OpenERP 7

I need to control visibility of the field based on the value of another field. That another field is a reference. I think I need to do a lookup pretty much the same way as I can do in a module with browse or search methods. But how to do in a view?
View:
<field name="org_no" attrs="{'invisible':[('country_id','!=','Sweden')]}"/>
Model (standard res.partner):
country_id: fields.many2one('res.country', 'Country')
field_view_get is static solution problem where once you set the value on domain, view will follow that, so if that's your requirement you can use it.
or else you can use below solution for better and dynamic behavior.
Take one Boolean field on your object where you wanna control visibility and add it in field as invisible.
Write on_change method of your field based on what you wanna control visibility and using the on change method you can set the value of the above boolean field.
using the First step boolean field on attrs to make field visible on demand.
Thank You
You can directly use this:
<field name="org_no" attrs="{'invisible':[('country_id.name','!=','Sweden')]}"/>
because country_id just stores the ID of the country that is selected in your many2one field
Leaving aside the hard coding, you pretty much have it. Just ensure that the country_id field is on the form so it can be used in the attrs on org_no. If you don't want the user to see the country, just put it on as an invisible field.

Explanation about widget="selection"

I need to know what is the actual scenario with widget option in OpenERP 7.
is it works as readonly field when we use it in form views ?
i used it in my one of form.when i save records in form, that widget values are not saved.
<field name="job_position" placeholder="Finance Manager" widget="selection" />
With widget, one can change the look of the field. In OpenERP, one can use such kind of many widget like widget="selection"/"statusbar"/"monetory"/"progressbar"/"html"/"email"/"image", etc. but it does not work as readonly field.
widget="selection" means it will show all the records of position(many2one) in the selection box. It means if you do not want any user to edit/modify it's record, widget="selection" is useful.
Regarding your issue, issue is due to placeholder attribute, that you used. Place holder will display that value in selection box which may not store in your many2one table and that's why while saving the record, value disappears. Try by removing placeholder over there. If you want any value by default in selection , use _defaults attribute in class.

Passing a model instance, not __unicode__ method in django

I've got a django form that contains a join via a foreign key. This is a foreign key to a very large table. On the form, to prevent loading up a massive select that tends to crash browsers, I've got a jQuery autocomplete, which on each keystroke sends off the entered text. This text is then searched in the table and suitable results are returned to be displayed. The id is then passed to a hidden CharField when one is selected. This hidden CharField is the widget for the ForeignKey relation. When I try to save the form, I get an error that I need to be passing a model instance for the related model, which is fair enough. I can't work out how to do this however. I can take that id and do a model.objects.get(pk=id_from_form), but if I replace the POST data with the result of this, I still get an error as I'm just passing the __unicode__ method of the model. I'm sure there's something I'm missing, but I can't see what it is.
Thanks.
Instead of using a CharField to store the id, try using a ModelChoiceField with the widget set as a HiddenInput. The field definition in your form would look something like:
mymodel = forms.ModelChoiceField(widget=forms.HiddenInput, queryset=MyModel.objects.all())

Categories

Resources