[Twisted-Python] Woven using wrong model adapter
![](https://secure.gravatar.com/avatar/9011310c6d9d5fcbf4b52d9d96ceb5e8.jpg?s=120&d=mm&r=g)
I have: components.registerAdapter(model.ObjectWrapper, interests.InterestChangeReport, wovenInterfaces.IModel) class Page(page.Page): def wmfactory_report(self, request): if sometimes: return deferredInterestChangeReport() else: return '' If the first page I serve after starting the server gets an InterestChangeReport, then everything works just fine. However, if the first page served gets '', then any future page that gets an InterestChangeReport dies with an exception: "the submodel was not found in [<twisted.web.woven.model.StringModel instance at 0x85167c4: wrapped data: <InterestChangeReport instance...>" So why does woven persist in using the StringModel adapter in this case, and how can I get it to stop? Don't know if this is related or not, but the log shows messages like: "Warning: There was no parent for node [...]; node not mutated." Is that something I should be concerned about? Thanks, - Kevin -- The moon is waning crescent, 16.8% illuminated, 25.6 days old.
![](https://secure.gravatar.com/avatar/3a7e70f3ef2ad1539da42afc85c8d09d.jpg?s=120&d=mm&r=g)
On 2003.05.26 21:13, Kevin Turner wrote:
I have:
components.registerAdapter(model.ObjectWrapper, interests.InterestChangeReport, wovenInterfaces.IModel)
class Page(page.Page): def wmfactory_report(self, request): if sometimes: return deferredInterestChangeReport() else: return ''
...
So why does woven persist in using the StringModel adapter in this case, and how can I get it to stop?
IIRC, A workaround is to 1) put the conditional in a special wvfactory rather than/in addition to your wmfactory, or 2) call self.invalidateCache() every time the result of the conditional changes (or just on every request, if you don't care)
Don't know if this is related or not, but the log shows messages like: "Warning: There was no parent for node [...]; node not mutated." Is that something I should be concerned about?
dunno -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://twistedmatrix.com/users/radix.twistd/
![](https://secure.gravatar.com/avatar/9011310c6d9d5fcbf4b52d9d96ceb5e8.jpg?s=120&d=mm&r=g)
On Mon, 2003-05-26 at 18:37, Christopher Armstrong wrote:
So why does woven persist in using the StringModel adapter in this case, and how can I get it to stop?
IIRC, A workaround is to [...] 2) call self.invalidateCache() every time the result of the conditional changes (or just on every request, if you don't care)
Model.invalidateCache is not what's needed here. It turns out that when that cache gets stale (either through explicit invalidateCache or just a new Request), Model.getData will update the model data in Model.original... but it only swaps out the Model.original attribute, it doesn't replace the Model with a new Model adapter for the new data. Which is how I was getting my FooReport instances ending up inside a StringModel adapter. What I've done now is, in Page.request: del self.submodels["report"] This makes sure that old model adapters never hang around. It also smells bad. Am I violating some unwritten contract here which says "a model factory must always return models with the same interface"? -- The moon is waning crescent, 15.6% illuminated, 25.7 days old.
![](https://secure.gravatar.com/avatar/74bde9dd414c74dc244c6a448e5decc6.jpg?s=120&d=mm&r=g)
On Monday, May 26, 2003, at 10:02 PM, Kevin Turner wrote:
Am I violating some unwritten contract here which says "a model factory must always return models with the same interface"?
A model factory must always return an object with the same identity within one request. See DeferredModel for an example of how to change the behaviour of an IModel implementor without changing it's identity. You really shouldn't be doing conditional rendering with a wmfactory anyway; put the conditional in the View, where it belongs. dp
![](https://secure.gravatar.com/avatar/3a7e70f3ef2ad1539da42afc85c8d09d.jpg?s=120&d=mm&r=g)
On 2003.05.27 01:56, Donovan Preston wrote:
On Monday, May 26, 2003, at 10:02 PM, Kevin Turner wrote:
Am I violating some unwritten contract here which says "a model factory must always return models with the same interface"?
A model factory must always return an object with the same identity within one request.
With such a restriction, it seems there's no reason to *call* the model factory multiple times per request. Shouldn't this be prevented? -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://twistedmatrix.com/users/radix.twistd/
![](https://secure.gravatar.com/avatar/74bde9dd414c74dc244c6a448e5decc6.jpg?s=120&d=mm&r=g)
On Tuesday, May 27, 2003, at 12:54 AM, Christopher Armstrong wrote:
On 2003.05.27 01:56, Donovan Preston wrote:
On Monday, May 26, 2003, at 10:02 PM, Kevin Turner wrote:
Am I violating some unwritten contract here which says "a model factory must always return models with the same interface"?
A model factory must always return an object with the same identity within one request.
With such a restriction, it seems there's no reason to *call* the model factory multiple times per request. Shouldn't this be prevented?
Correct. The wmfactory method isn't called more than once per request. It's getSubmodel that has this restriction, sorry. dp
participants (3)
-
Christopher Armstrong
-
Donovan Preston
-
Kevin Turner