import a model ?

61 views
Skip to first unread message

Tim Richardson

unread,
Oct 3, 2013, 9:46:37 PM10/3/13
to web...@googlegroups.com
If I have a conditional model, that is a model in a folder named after its controller, how can another controller source that model?

Anthony

unread,
Oct 3, 2013, 10:24:51 PM10/3/13
to web...@googlegroups.com
You can customize response.models_to_run.

Anthony

Tim Richardson

unread,
Oct 3, 2013, 10:41:39 PM10/3/13
to web...@googlegroups.com
Thanks. I'm trying that but not getting it to work for me, maybe I have another problem.

The problem is a function in controller C1 which refers to a part of the model for C2. Both controllers have models in models/C1 and models/C2 

Right now I have at the top of C1
response.models_to_run['.*']  (just for debugging) but I still get a ticket saying that 

<type 'exceptions.AttributeError'> 'DAL' object has no attribute 'patient_submission'

where patient_submission is the table defined in the model models/C2/C2_db.py

I know that response.models_to_run is being changed thanks to the debugger.


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/OiZ6vwaNuEU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Tim Richardson

Vinicius Assef

unread,
Oct 3, 2013, 10:53:54 PM10/3/13
to web2py
reponse.models_to_run must be updated on models, before controller execution.
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Anthony

unread,
Oct 3, 2013, 10:55:04 PM10/3/13
to web...@googlegroups.com

Right now I have at the top of C1
response.models_to_run['.*']  (just for debugging) but I still get a ticket saying that 

Are you saying you put that line at the top of the C1 controller? If so, that's too late -- the controller isn't run until after the models have been executed. You have to set/change response.models_to_run in one or more model files. By default:

response.models_to_run = [r'^\w+\.py$', r'^%s/\w+\.py$' % request.controller,
                          r
'^%s/%s/\w+\.py$' % (request.controller, request.function)]

which sets up the standard conditional models.

Anthony

Vinicius Assef

unread,
Oct 3, 2013, 11:00:52 PM10/3/13
to web2py
An alternative would be something like this:
what_models_to_be_executed = {"C1": ["tab1.py", "tab2.py", "tab3.py"],
"C2": "tab2.py"}
response.models_to_run = what_models_to_be_executed[request.controller]
> --
> Resources:
> - http://web2py.com
> - http://web2py.com/book (Documentation)
> - http://github.com/web2py/web2py (Source code)
> - https://code.google.com/p/web2py/issues/list (Report Issues)
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Tim Richardson

unread,
Oct 3, 2013, 11:02:21 PM10/3/13
to web...@googlegroups.com

I put it at the top of the C1 model and I know it is being executed...  I'll step through more and see what's happening

--

Anthony

unread,
Oct 3, 2013, 11:09:23 PM10/3/13
to web...@googlegroups.com
An alternative would be something like this: 
what_models_to_be_executed = {"C1": ["tab1.py", "tab2.py", "tab3.py"],
"C2": "tab2.py"}
response.models_to_run = what_models_to_be_executed[request.controller]

Note, it must be a list (above, in the case of C2, it would just be a string).

Anthony

unread,
Oct 3, 2013, 11:30:13 PM10/3/13
to web...@googlegroups.com
Never mind -- it does not have to be a list -- can just be a string. 

Tim Richardson

unread,
Oct 3, 2013, 11:37:38 PM10/3/13
to web...@googlegroups.com
I have response.models_to_run = ['.*']
at the top of the C1/C1_db model.
When I put a breakpoint, it completes the rest of the C1_db model, and arrives at compileapp.py at a loop
for model in models:
 
this is the loop where the regex is evaluated.
But it doesn't enter the loop.
I'm guessing that perhaps I have reentered that loop on the last iteration of models.

Anyway, it never evaluates my new regex. 

I have been misleading with my example names: I say C1 but alphabetically the model with my response.models_to_run is actually later in the alphabet.It is the final model. The model I want to execute has alphabetic precedence.  
Perhaps this is the problem: the use of response.models_to_run does not regenerate the list of models, it just changes the test to execute a model from a candidate list, but if a candidate has already been evaluated it doesn't get looked at again. 



--
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/OiZ6vwaNuEU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Tim Richardson

Anthony

unread,
Oct 3, 2013, 11:39:19 PM10/3/13
to web...@googlegroups.com
Oh, it looks like the issue regarding sorting of model folders was never fixed.

Massimo, can we change to sort=True in compileapp.py lines 517 and 521?

Anthony

Tim Richardson

unread,
Oct 3, 2013, 11:44:25 PM10/3/13
to web...@googlegroups.com

 Yes, I am correct. That's a bit subtle. By making a model folder ZZZ and putting a copy of the requested model in there, it works. Not ideal. Possibly could be a bug, maybe. Or perhaps needs to be better documented. 
--
Tim Richardson

Tim Richardson

unread,
Oct 3, 2013, 11:50:42 PM10/3/13
to web...@googlegroups.com
It doesn't seem to me to be a sort order problem. The problem seems to me to be different: the criteria for evaluating which models to execute can change while in that loop. If the criteria changes, then all candidate models (all models in other words) should be reevaluated. The loop index should reset to one. I don't know enough python yet.

Tim Richardson

unread,
Oct 4, 2013, 12:03:00 AM10/4/13
to


On Friday, 4 October 2013 13:50:42 UTC+10, Tim Richardson wrote:
It doesn't seem to me to be a sort order problem. The problem seems to me to be different: the criteria for evaluating which models to execute can change while in that loop. If the criteria change, then all candidate models (all models in other words) should be reevaluated. The loop index should reset to one. I don't know enough python yet.


I don't think it's such an easy fix because it is a question of dependency resolution. 

My work around will be to put common models in a ZZZ folder 

Tim Richardson

unread,
Oct 4, 2013, 12:38:10 AM10/4/13
to web...@googlegroups.com

Anthony

unread,
Oct 4, 2013, 8:55:26 AM10/4/13
to web...@googlegroups.com
I commented on the issue, but this is the intended behavior. The order of execution is fixed (though you can determine that fixed order with proper naming of files and folders) -- response.models_to_run is only intended for specification of which files get executed, not in which order. Do you have a use case where you need to dynamically change the order of model execution?

I think what you call a "workaround" (i.e., putting the model file that is common to two controllers in its own folder) is actually the cleaner design -- that model file doesn't belong to only one controller or the other, so it doesn't belong in a folder associated with only one of the controllers.

Anthony

On Friday, October 4, 2013 12:38:10 AM UTC-4, Tim Richardson wrote:

Anthony

unread,
Oct 4, 2013, 8:56:12 AM10/4/13
to web...@googlegroups.com
And of course, you can always put model code in a module and import it where needed.

Tim Richardson

unread,
Oct 4, 2013, 10:06:35 AM10/4/13
to web...@googlegroups.com
Thanks. I'll try import next time.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/OiZ6vwaNuEU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
Tim Richardson

Jonathan Lundell

unread,
Oct 4, 2013, 10:32:51 AM10/4/13
to web...@googlegroups.com
Mutatis mutandis…
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages