Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>"create a python script and install that script as a cron (with DJANGO _SETTINGS _MODULE variable setup before executing the script)?" </p> <p>First, be sure to declare your Forms in a separate module (e.g. <code>forms.py</code>) </p> <p>Then, you can write batch loaders that look like this. (We have a LOT of these.)</p> <pre><code>from myapp.forms import MyObjectLoadForm from myapp.models import MyObject import xml.etree.ElementTree as ET def xmlToDict( element ): return dict( field1= element.findtext('tag1'), field2= element.findtext('tag2'), ) def loadRow( aDict ): f= MyObjectLoadForm( aDict ) if f.is_valid(): f.save() def parseAndLoad( someFile ): doc= ET.parse( someFile ).getroot() for tag in doc.getiterator( "someTag" ) loadRow( xmlToDict(tag) ) </code></pre> <p>Note that there is very little unique processing here -- it just uses the same Form and Model as your view functions. </p> <p>We put these batch scripts in with our Django application, since it depends on the application's <code>models.py</code> and <code>forms.py</code>.</p> <p>The only "interesting" part is transforming your XML row into a dictionary so that it works seamlessly with Django's forms. Other than that, this command-line program uses all the same Django components as your view.</p> <p>You'll probably want to add options parsing and logging to make a complete command-line app out of this. You'll also notice that much of the logic is generic -- only the <code>xmlToDict</code> function is truly unique. We call these "Builders" and have a class hierarchy so that our Builders are all polymorphic mappings from our source documents to Python dictionaries.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload