在模板django中显示一对多的关系关系

时间:2018-01-25 17:07:19

标签: django templates relationship

我的model.py中有模型: 我想在网页中显示和编辑这两个模型。 有人可以帮帮我。

  1. parent:fdt_schedulejour
  2. fdt_schedulejour

    class fdt_schedulejour(models.Model):
        user = models.ForeignKey(User)
        date = models.DateField(blank=True, null=True)
        commentaire = models.CharField(max_length=500, blank=True, null=True)
    
        class Meta:
            managed = False
            db_table = 'fdt_schedulejour'
    
        id = models.AutoField(primary_key=True)
        def __unicode__(self):
            return '%s' % (self.date)
    
    1. child:fdt_activite。
    2. fdt_schedulejour

      class fdt_activite(models.Model):
          schedulejour = models.ForeignKey(fdt_schedulejour)
          debut = models.TimeField(blank=True, null=True)
          fin = models.TimeField(blank=True, null=True)
      
          class Meta:
              managed = False
              db_table = 'fdt_activite'
      
          id = models.AutoField(primary_key=True)
          def __unicode__(self):
              return '%s' % (self.type) 
      

      谢谢

1 个答案:

答案 0 :(得分:0)

我的观点:

def add(request):
    if request.method != "POST":
        messages.error(request,"Ajout impossible. Corrigez l'erreur et recommencez svp !!!")
        return redirect('/')
    else:
    add_appoint= fdt_schedulejour.objects.appointval(request.POST, request.session['id'])
    if add_appoint[0] == False:
        for each in add_appoint[1]:
            messages.error(request, each)
        return redirect('/appoint')
    if add_appoint[0] == True:
        messages.success(request, 'Ajout reussi avec succes')
        return redirect('/appoint')

模板:

<div class="container">
    <h2>Ajoutez vos heures </h2>
    {% if messages %}
        <ul class="messages">
        {% for message in messages %}
        <li style="color:red">{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    <form action="/add" class="col s12" method="POST">
        {% csrf_token %}

      <!-- PARENT: FDT_SCHEDULEJOUR -->
      <div class="row">
        <div class="input-field col s12">
          <input id="date" type="date" class="form-control" name="date">
          Date
        </div>
      </div>

      <!-- CHILD: FDT_ACTIVITE -->
      <!--   NOT WORK -->
      <div class="row">
        <div class="input-field col s12">
          <input id="debut" type="time" step="any" class="form-control" name="debut">
          debut
        </div>
      </div>
      <div class="row">
        <div class="input-field col s12">
          <input id="fin" type="time" step="any" class="form-control" name="fin">
          fin
        </div>
      </div>

      <input type="submit" name="add" value="AJOUTER">
  </form>
</div>