<p>I'm a firm believer of the heretic thought of tight coupling between the backend and frontend: I want existing, implied knowledge about a backend to be automatically made use of when generating user interfaces. E.g., if a VARCHAR column has a maximum with of 20 characters, there GUIs should automatically constrain the user from typing more than 20 characters in a related form field.</p>

<p>And I have strong antipathy to ORMs which want to define my database tables, or are based on some hack where every table needs to have extra numeric ID columns because of the ORM.</p>

<p>I've looked a bit into Python database frameworks and I think I can conclude the SQLAlchemy fits best to my mentality.</p>

<p>Now, I need to find a web application framework which fits naturally with SQLAlchemy (or an equivalent) and perhaps even with my appetite for coupling. With "web application framework", I mean products/project such as Pyhons, Django, TurboGears, web2py, etc.</p>

<p>E.g., it should ideally be able to:</p>

<ul>
<li><strong>automatically select a suitable form widget</strong> for data entering a given column if told to do so; e.g., if the column has a foreign key to a column with 10 different values, widget should display the 10 possible values as a dropdown</li>
<li><strong>auto-generate javascript form validation code</strong> which gives the end-user quick error feedback if a string is entered into a field which is about to end up in an INTEGER column, etc</li>
<li>auto-generate a <strong>calendar widget</strong> for data which will end up in a DATE column</li>
<li><strong>hint NOT NULL constraints</strong> as javascript which complains about empty or whitespace-only data in a related input field</li>
<li>generate javascript validation code which matches relevant (simple) <strong>CHECK-constraints</strong></li>
<li>make it easy to <strong>avoid SQL injection</strong>, by using prepared statements and/or validation of externally derived data</li>
<li>make it easy to <strong>avoid cross site scripting</strong> by automatically escape outgoing strings when appropriate</li>
<li><strong>make use of constraint names</strong> to generate somewhat user friendly error messages in case a constrataint is violated</li>
</ul>

<p>All this should happen dynamically, so table adjustments are automatically reflected on the frontend - probably with a caching mechanism, so that all the model introspection wouldn't kill performance. In other words, I don't want to repeat my model definition in an XML file (or alike) when it has already been carefully been defined in my database.</p>

<p>Does such a framework exist for Python (or for any language, for that matter)? If not: Which of the several Python web application frameworks will be least in the way if I were to add parts of the above features myself?</p>

[{'answer_id': 43386, 'author': 'Peter Hoffmann', 'author_id': 720, 'author_profile': 'https://Stackoverflow.com/users/720', 'pm_score': 2, 'selected': False, 'text': '<p>You should have a look at django and especially its <a href="http://www.djangoproject.com/documentation/forms/" rel="nofollow noreferrer">newforms</a> and <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#ref-contrib-admin" rel="nofollow noreferrer">admin</a> modules. The newforms module provides a nice possibility to do server side validation with automated generation of error messages/pages for the user. Adding ajax validation is also <a href="http://lukeplant.me.uk/blog.php?id=1107301681" rel="nofollow noreferrer">possible</a> </p>\n'}, {'answer_id': 43414, 'author': 'codeape', 'author_id': 3571, 'author_profile': 'https://Stackoverflow.com/users/3571', 'pm_score': 1, 'selected': False, 'text': '<p>I believe that Django models does not support composite primary keys (see <a href="http://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields" rel="nofollow noreferrer">documentation</a>). But perhaps you can use SQLAlchemy in Django? A <a href="http://www.google.com/search?q=sqlalchemy+django" rel="nofollow noreferrer">google search</a> indicates that you can. I have not used Django, so I don\'t know.</p>\n\n<p>I suggest you take a look at:</p>\n\n<ul>\n<li><a href="http://toscawidgets.org/" rel="nofollow noreferrer">ToscaWidgets</a></li>\n<li><a href="http://code.google.com/p/dbsprockets/" rel="nofollow noreferrer">DBSprockets</a>, including <a href="http://code.google.com/p/dbsprockets/wiki/DBMechanic" rel="nofollow noreferrer">DBMechanic</a></li>\n<li><a href="http://www.checkandshare.com/catwalk/" rel="nofollow noreferrer">Catwalk</a>. Catwalk is an application for TurboGears 1.0 that uses SQLObject, not SQLAlchemy. Also check out this <a href="http://www.checkandshare.com/blog/?p=41" rel="nofollow noreferrer">blog post</a> and <a href="http://www.checkandshare.com/CATWALK2/lview/index.html" rel="nofollow noreferrer">screencast</a>.</li>\n<li><a href="http://docs.turbogears.org/1.0/DataController" rel="nofollow noreferrer">FastData</a>. Also uses SQLObject.</li>\n<li><a href="http://code.google.com/p/formalchemy/" rel="nofollow noreferrer">formalchemy</a></li>\n<li><a href="http://rumdemo.toscawidgets.org/" rel="nofollow noreferrer">Rum</a></li>\n</ul>\n\n<p>I do not have any deep knowledge of any of the projects above. I am just in the process of trying to add something similar to one of my own applications as what the original question mentions. The above list is simply a list of interesting projects that I have stumbled across.</p>\n\n<p>As to web application frameworks for Python, I recommend TurboGears 2. Not that I have any experience with any of the other frameworks, I just like TurboGears...</p>\n\n<p>If the original question\'s author finds a solution that works well, please update or answer this thread.</p>\n'}, {'answer_id': 48284, 'author': 'andy47', 'author_id': 2661, 'author_profile': 'https://Stackoverflow.com/users/2661', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://www.turbogears.org/" rel="nofollow noreferrer">TurboGears</a> currently uses <a href="http://www.sqlobject.org/" rel="nofollow noreferrer">SQLObject</a> by default but you can use it with <a href="http://docs.turbogears.org/1.0/SQLAlchemy" rel="nofollow noreferrer">SQLAlchemy</a>. They are saying that the next major release of TurboGears (1.1) will use SQLAlchemy by default.</p>\n'}, {'answer_id': 48479, 'author': 'Jason', 'author_id': 5036, 'author_profile': 'https://Stackoverflow.com/users/5036', 'pm_score': 1, 'selected': False, 'text': "<p>I know that you specificity ask for a framework but I thought I would let you know about what I get up to here. I have just undergone converting my company's web application from a custom in-house ORM layer into sqlAlchemy so I am far from an expert but something that occurred to me was that sqlAlchemy has types for all of the attributes it maps from the database so why not use that to help output the right html onto the page. So we use sqlAlchemy for the back end and Cheetah templates for the front end but everything in between is basically our own still.</p>\n\n<p>We have never managed to find a framework that does exactly what we want without compromise and prefer to get all the bits that work right for us and write the glue our selves. </p>\n\n<p>Step 1. For each data type sqlAlchemy.types.INTEGER etc. Add an extra function toHtml (or many maybe toHTMLReadOnly, toHTMLAdminEdit whatever) and just have that return the template for the html, now you don't even have to care what data type your displaying if you just want to spit out a whole table you can just do (as a cheetah template or what ever your templating engine is).</p>\n\n<p>Step 2</p>\n\n<p><code>&lt;table&gt;</code></p>\n\n<p><code>&lt;tr&gt;</code></p>\n\n<p><code>#for $field in $dbObject.c:</code></p>\n\n<p><code>&lt;th&gt;$field.name&lt;/th&gt;</code></p>\n\n<p><code>#end for</code></p>\n\n<p><code>&lt;/tr&gt;</code></p>\n\n<p><code>&lt;tr&gt;</code></p>\n\n<p><code>#for $field in dbObject.c:</code></p>\n\n<p><code>&lt;td&gt;$field.type.toHtml($field.name, $field.value)&lt;/td&gt;</code></p>\n\n<p><code>#end for</code></p>\n\n<p><code>&lt;/tr&gt;</code></p>\n\n<p><code>&lt;/table&gt;</code></p>\n\n<p>Using this basic method and stretching pythons introspection to its potential, in an afternoon I managed to make create read update and delete code for our whole admin section of out database, not yet with the polish of django but more then good enough for my needs.</p>\n\n<p>Step 3 Discovered the need for a third step just on Friday, wanted to upload files which as you know needs more then just the varchar data types default text box. No sweat, I just overrode the rows class in my table definition from VARCHAR to FilePath(VARCHAR) where the only difference was FilePath had a different toHtml method. Worked flawlessly.</p>\n\n<p>All that said, if there is a shrink wrapped one out there that does just what you want, use that.</p>\n\n<p>Disclaimer: This code was written from memory after midnight and probably wont produce a functioning web page.</p>\n"}, {'answer_id': 198348, 'author': 'massimo', 'author_id': 24489, 'author_profile': 'https://Stackoverflow.com/users/24489', 'pm_score': 3, 'selected': False, 'text': "<p>web2py does most of what you ask:</p>\n\n<p>Based on a field type and its validators it will render the field with the appropriate widget. You can override with</p>\n\n<pre><code>db.table.field.widget=...\n</code></pre>\n\n<p>and use a third party widget.</p>\n\n<p>web2py has js to blocks the user from entering a non-integer in a integer field or a non-double in a double field. time, date and datetime fields have their own pickers. These js validation work with (not instead) of server side validation.</p>\n\n<p>There is <code>IS_EMPTY_OR(...)</code> validator.</p>\n\n<p>The DAL prevents SQL injections since everthing is escaped when goes in the DB.</p>\n\n<p>web2py prevents XSS because in {{=variable}}, 'variable' is escaped unless specified otherwise {{=XML(variable)}} or {{=XML(variable,sanitize=True)}}</p>\n\n<p>Error messages are arguments of validators for example</p>\n\n<pre><code>db.table.field.requires=IS_NOT_EMPTY(error_message=T('hey! write something in here'))\n</code></pre>\n\n<p>T is for internationalization.</p>\n"}]
['https://Stackoverflow.com/questions/43368', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4462/']