Martinho
Martinho

Reputation: 389

Appengine forms Option Select from db.Model

My application does not write all data in the database. In the example below just type in the DB name. All fields select dropdown are not recorded in the database. Help please

I have a models Docente

ESCOLHA_SEXO = (u'masculino', u'feminino')
CHOICES_UNIDADE = ('Escola Superior de Tecnologia', 'Escola Superior de Gestao') 
CHOICES_CATEGORIA = ('assistente', 'coordenador', 'adjunto')
CHOICES_REGIME = ('trinta', 'cinquenta', 'sessenta', 'cem')

class Docente(db.Model):
    photo=db.BlobProperty(u'photo')
    docente_nome = db.StringProperty(u'docente_nome', 
                                     required=False)
    docente_unidade = db.StringProperty(u'docente_unidade', 
                                        required=False, 
                                        default='Escola Superior de Tecnologia', 
                                        choices = CHOICES_UNIDADE )
    docente_categoria = db.StringProperty(u'docente_categoria', 
                                          default='assistente', 
                                          choices =CHOICES_CATEGORIA)
    docente_regime = db.StringProperty(u'docente_regime', 
                                       required=False, 
                                       choices =CHOICES_REGIME)
    utilizador=db.ReferenceProperty(Utilizador, 
                                    verbose_name=u'utilizador', 
                                    required=False, 
                                    collection_name='utilizadores')

This is my main:

class CriarCvHandler(webapp.RequestHandler):
    def post(self):
        if self.request.get('EscDocente'):
            id = int(self.request.get('EscDocente'))
            docente=models.Docente.get(db.Key.from_path('Docente', id))
        else:
            docente = models.Docente()

        data=forms.DocenteForm(data = self.request.POST)
        if data.is_valid():
            if self.request.get('photo'):
                docente.docente_nome=self.request.get('docente_nome')
                docente.docente_unidade=self.request.get('docente_unidade')
                docente.docente_categoria=self.request.get('docente_categoria')
                docente.docente_regime=self.request.get('docente_regime')

            listaUtlz = models.Utilizador.all()
            listaUtlz.filter('user =', users.get_current_user())

            for utilizador in listaUtlz:
                docente.utilizador=utilizador.key()

            docente.put()
            self.redirect('/academia')
        else:
            self.redirect('/criarCv')

    def get(self):
        user=users.get_current_user()
        if user:
            greeting= ("<ul><li><strong><b>Benvindo %s </b></strong>|</li><li><a href=\"/perfil\"> Minha Conta </a>|</li><li><a href=\"%s\"> Logout</a></li></ul>" %(user.nickname(), users.create_logout_url("/")))
        else:
            greeting = ("<ul><li><a href=\"%s\">Login</a></li></ul>" %(users.create_login_url("/")))

        conjUnidade=models.Docente.docente_unidade.choices
        conjCategoria=models.Docente.docente_categoria.choices
        conjRegime=models.Docente.docente_regime.choices

        utilizador=db.Query(models.Utilizador)
        utilizador=utilizador.filter('user =', user)
        listaUtlz=utilizador.fetch(limit=1)

        if self.request.GET.has_key('id'):
            id=int(self.request.GET['id'])
            EscDocente=models.Docente.get(db.Key.from_path('Docente', id))

        path = os.path.join(os.path.dirname(__file__), 'templates/criarCv.html')
        self.response.out.write(template.render(path, locals(), debug = True))

This is my template: Nome Docente:

<tr>
  <td>Unidade: </td>
  <td>
    <select name="docente_unidade">
      {% for docente_unidade in conjUnidade  %}
        <option selected> {{ docente_unidade }} </option>
      {% endfor %}
    </select>
  </td>
</tr>

<tr>
  <td>Categoria: </td>
  <td>
    <select name="categoria">
      <option></option>
        {% for docente_categoria in conjCategoria  %}
          {% ifequal EscDocente.docente_categoria docente_categoria %}
            <option selected> {{ docente_categoria }} </option>
          {% else %}
            <option> {{ docente_categoria }} </option>
          {% endifequal %}
        {% endfor %}
    </select>
  </td>
</tr>

<td>Regime: </td>
  <td>
    <select name="regime">
      <option></option>
        {% for docente_regime in conjRegime  %}
          {% ifequal EscDocente.docente_regime docente_regime %}
            <option selected> {{ docente_regime }} </option>
          {% else %}
            <option> {{ docente_regime }} </option>
          {% endifequal %}
        {% endfor %}
    </select>
  </td>

  <tr>
    <td></td>
    <td>
      <input type="button" value="Guardar" onclick="guardaCv()" />

Upvotes: 0

Views: 227

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169334

Change these:

<select name="categoria">

...

<select name="regime">

To this:

<select name="docente_categoria">

...

<select name="docente_regime">

Upvotes: 1

Related Questions