title: Appengine Data Validation Pattern?
body: <p>I am pretty new to appengine and I am interested to hear what are the best practices when it comes to data validation. </p>
  
  <p>First off, this is my models.py</p>
  
  <pre><code>class Event(db.Model):  
    name = db.StringProperty()
    event_type = db.ReferenceProperty(EventType)
    data = db.TextProperty()
  </code></pre>
  
  <p>Ideally, I would like to enforce <code>name</code> and <code>event_type</code> to have <code>required=True</code>.</p>
  
  <p>However, I also come from a rails background and this is my edit_event.html (used for both creating and editing a event):</p>
  
  <pre><code>&lt;form method="POST"&gt;   
  
    name: &lt;input type="text" name="name" value="{{ event.name or '' }}"/&gt;
    &lt;br/&gt;
  
    event type:
    &lt;select name="event_type"&gt;
      {% for event_type in event_types %}
        &lt;option value={{ event_type.key().id_or_name() }}&gt;
        {{- event_type.name -}}
        &lt;/option&gt;
      {% endfor %}
    &lt;/select&gt;
    &lt;br/&gt;
  
    data: &lt;input type="text" name="data" value="{{ event.data or '' }}"/&gt;
    &lt;br/&gt;
  
    &lt;input type="submit" value="Submit"/&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>And this is what my view function looks like:</p>
  
  <pre><code>@app.route('/events/new/', methods=['GET', 'POST'])
  @login_required
  def new_event():
    if request.method == 'GET':
      return render_template('edit_event.html',
                             event=Event(),
                             event_types=db.Query(EventType).fetch(None))
    else:
      _update_event(Event(), request.form)
      return redirect(url_for('list_events'))
  
  
  @app.route('/events/&lt;int:event_id&gt;/', methods=['GET', 'POST'])
  @login_required
  def edit_event(event_id):
    event = Event.get_by_id(event_id)
    if request.method == 'GET':
      return render_template('edit_event.html',
                             event=event,
                             event_types=db.Query(EventType).fetch(None))
    else:
      _update_event(event, request.form)
      return redirect(url_for('list_events'))
  </code></pre>
  
  <p>(NOTE: I've left out _update_event for the sake of breivity)</p>
  
  <p>As you can see, if I enforce <code>required=True</code> in the model, <code>event=Event()</code> in <code>new_event()</code> will raise an Exception. </p>
  
  <p>If I don't include <code>event=Event()</code> in <code>new_event()</code>, I am getting an error that <code>event is not defined</code>. </p>
  
  <p>What is a good way to have both required=True and a reusable edit_event.html that works for both new_event and edit_event? </p>
  
  <p><strong>OF COURSE, if I have <code>required=True</code> and <code>default=X</code> it's kind of defeating the purpose.</strong></p>