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.
Related
I'm attempting to create a form that allows someone to enter multiple phone numbers. I'd like to make it possible to click a button to add an additional field to the form but I am not quite sure how to do that.
I looked at FormSets but I don't want to create a new model instance every time someone adds a new phone number.
Thanks in advance
One way is, you could consider using an ArrayField or JSONField for the phone numbers. The latter might be more useful if you want to always be able to easily reference the numbers e.g
{'primary_no': '+123232323'}
Then for the frontend you can use a button with Javascript to 'clone' the input element (or better create a new one with the same class names) and append it to a phone number wrapper div on click. But you'll need to take note to use the same field name like one would for a checkbox element e.g
<input type="text" name="phone_number[]" />
This would make the values of these fields be sent in an array for request.POST.getlist('phone_number[]'), which you can then loop through and validate. I left out the nitty gritty details here but this should give you an idea.
In res.partner form view of sale.order, you got this code:
<field name="partner_id" on_change="onchange_partner_id(partner_id, context)" domain="[('customer','=',True)]" context="{'search_default_customer':1, 'show_address': 1}" options='{"always_reload": True}'/>
This view takes res.partner address into form ie: context="{'search_default_customer':1, 'show_address': 1}" now, I want this same behavior BUT on tree view.
I just copied this context into tree view, on partner_id field, but nothing happens, I tried many ways, with no results.
I'd like to have the partner's address on tree view also.
Anybody knows how to accomplish this?
Thanks in advance!
As per my point of view, context is used for often available as contextual actions on resources;
context is nothing but a python directory.
In your case this context will be update with your predefined value like here you have update it with 2 keys.
so when you use any method on this field like on_change so in that case you can use this updated context.
I'm facing a complex problem, at least for me.
I have a form called "Task", which contains all the normal info, and I would like to add users to that Task.
If I want to add multiple users to that task, I should use the widget one2many, am I right? If so, is it possible to display a dropdown or something and add the users already registered? Because, with the default one2many, I have to register the users (like a Form) and then I can add them..but if they are already in the table, it should appear me a dropdown menu or something..
After the task is created, the users should only see the task with their name, only administrator can view it all. I think that to achieve this I need to create rules, right? If so, do I need to create them by code or could I use the openERP rule menu? And this will be enough: ('user_id', '=', user.id)]? The first column "user_id" is created on "Task" table?
I do not need to have a auxiliary table that would contain something like: id, task_id, id_user..and by this I could get which tasks belongs to whichs users??
Thanks guys
For your cases:
You can try using Many2Many relation so as to choose the record of users.
Use Groups to obtain your desired result.
For example:
<field name="user_id" groups="your_group" />
By doing this you can provide what fields to be visible to which user based on access rights provided in your GROUPS.
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.
I have a requirement where one user creates an 'instance' of an object via a ModelForm. Another user of a different group has access to read all of the fields of the form, but has to update only one field. Think of a student who creates an exam object. Then a teach pulls up the exam and just needs to put in a grade, the rest of the exam is read only.
What's the best way to do that? Should I just query for the object, and display each field individually, then create a form (not a ModelForm?) for just the one field?
Should I just query for the object, and display each field individually, then create a form (not a ModelForm?) for just the one field?
This is probably the best way to go about it. Note you can use a ModelForm for the teacher form, see the Django documentation on using a subset of fields on a model form. You will have to display all the other fields manually in your template, but you should probably have a separate template for this view (I would use separate views as well).
You could find some code for a read only field on Django Snippets, but generally it's better to be explicit about what fields you are updating from each view. This is likely to be more trouble than it's worth.