How to create and populate a Plone object in portal_factory?

I need to create an object (archetypes) from the View of the second object ( object2 - which is not the parent of the new object). It must be pre-populated with data from the Request and from object2 .

A simple solution would seem to be to use "default_method" in the schema fields, and this might work for the data from the request, but I do not believe that I have access to the view from there and therefore not to object2 . In any case, one of the fields is the ReferenceField for object2 , and I read that the ReferenceField ignores the "default_method".

Another option is to create it inside portal_factory, set its default values, and then display the add page, allowing the user to change the content as needed or exit without actually creating the object. Absolutely, in addition, from the many methods available for creating an object ( invokeFactory() , _createObjectByType() , _constructInstance() and createObject() , which I know), only createObject actually leaves the object in portal_factory - and since it only returns a string ( The URL of the Add page object) will not accept keyword arguments and does not seem to notify of any events (of course, not IObjectCreatedEvent), I don’t see how to change this using my data before directing the user to the page editing.

+4
source share
2 answers

This is the template that I recommend when it is not possible to use createObject:

 _id = self.context.generateUniqueId("Document") _id = self.context.invokeFactory(type_name=type_name, id=_id) ob = self.context[_id] ob.edit( description = "text...", subject = ('tag1', 'tag2'), title = "some title...", ) ob._renameAfterCreation(check_auto_id=True) _id = ob.getId() 
+4
source

Doh. Finally figured it out.

createObject in any sense does not create an object. This is really just the url of the creation form.

Call .createObject() , get the form URL, attach the values ​​you want as request parameters:

 url = folder.createObject(type_name='xxx') url += ?title=abc&description=def...' self.request.RESPONSE.redirect(url) 

will do it.

+4
source

Source: https://habr.com/ru/post/1379205/


All Articles