openerp context in act_window

2024/10/18 12:47:06

In OpenERP 6.1 this act_window:

<act_windowdomain="[('id', '=', student)]"id="act_schedule_student"name="Student"res_model="school.student"src_model="school.schedule"/>

creates a Student button in the Schedule form which opens the student tree view showing only the appropriate student.

My goal is to directly open the corresponding form view of the student instead of a tree view with the right filtered student. I tried adding a view_mode="form,tree" but it opens a new form instead of the one I want. I'm guessing this can possibly be achieved by adding context to the act_window? Maybe a record_id, but I tried that with active_id and it didn't work.

Answer

The magical (and probably undocumented) way to have an OpenERP action directly open the form view of a given record, is to set an extra res_id attribute on the action.

Unfortunately in OpenERP 6.1[1] the res_id attribute is not part of the act_window data model, so it is not possible to directly set it in an XML declaration.

Most official addons use a <button type="object" ... /> bound to a Python method that sets the res_id attribute in the returned action. It is quite easy to find examples of this in the source code of official modules, and you can see one in this related question.

Quick/untested example:

You would add this in your school.schedule form:

<button name="open_student_form" type="object" string="Student"/>

And the following method in the school.schedule model:

def open_student_form(self, cr, uid, ids, context=None):this = self.browse(cr, uid, ids, context=context)[0]return {'type': 'ir.actions.act_window','name': 'Student','view_mode': 'form','view_type': 'form','res_model': 'school.student','nodestroy': 'true','res_id': this.student.id, # assuming the many2one is (mis)named 'student''views': [(False, 'form')],}

Now if you really wanted to do this with a "sidebar button" (i.e. with an <act_window/>), it gets a bit trickier because you cannot directly bind a sidebar button to a Python method; it must be bound to an action that is stored in the database. It is still doable though, for example via an ir.actions.server action that can be bound to your <act_window/> and calls your Python method or does something similar. The trick with ir.actions.server is that it can be defined as a Python block that can return a dynamic action definition by assigning an action dictionary to an action variable. If you want to follow that path, search the OpenERP addons source code for declarations of ir.actions.server (some of them might do similar things) and methods returning actions with a res_id attribute.

[1] As of OpenERP 7.0 the res_id column is explicitly available in the data model, so you can directly set it.

https://en.xdnf.cn/q/73278.html

Related Q&A

Djangos redirects app doesnt work with URL parameters

I recently installed Djangos default redirects app on my site using the exact instructions specified:Ensured django.contrib.sites framework is installed. Added django.contrib.redirects to INSTALLED_APP…

get fully qualified method name from inspect stack

I have trouble completing the following function:def fullyQualifiedMethodNameInStack(depth=1):"""The function should return <file>_<class>_<method> for the method in th…

Project Euler #18 - how to brute force all possible paths in tree-like structure using Python?

Am trying to learn Python the Atlantic way and am stuck on Project Euler #18.All of the stuff I can find on the web (and theres a LOT more googling that happened beyond that) is some variation on well …

Is it possible to sniff the Character encoding?

I have a webpage that accepts CSV files. These files may be created in a variety of places. (I think) there is no way to specify the encoding in a CSV file - so I can not reliably treat all of them as …

numpy.empty giving nonempty array

When I create an empty numpy array using foo = np.empty(1) the resulting array contains a float64:>>> foo = np.empty(1) >>> foo array([ 0.]) >>> type(foo[0]) <type numpy.f…

Accessing password protected url from python script

In python, I want to send a request to a url which will return some information to me. The problem is if I try to access the url from the browser, a popup box appears and asks for a username and passwo…

Solving multiple linear sparse matrix equations: numpy.linalg.solve vs. scipy.sparse.linalg.spsolve

I have to solve a large amount of linear matrix equations of the type "Ax=B" for x where A is a sparse matrix with mainly the main diagonal populated and B is a vector. My first approach was …

I want to return html in a flask route [duplicate]

This question already has answers here:Python Flask Render Text from Variable like render_template(4 answers)Closed 6 years ago.Instead of using send_static_file, I want to use something like html(<…

Why doesnt cv2 dilate actually affect my image?

So, Im generating a binary (well, really gray scale, 8bit, used as binary) image with python and opencv2, writing a small number of polygons to the image, and then dilating the image using a kernel. Ho…

How to plot text clusters?

I have started to learn clustering with Python and sklearn library. I have wrote a simple code for clustering text data. My goal is to find groups / clusters of similar sentences. I have tried to plot…