Wednesday, November 7, 2018

Re: Form with two models

Hi.  Welcome to Django world.

You can use formset to show and manage both models, Specifically you can use inlineformset_factory, who can help you with that.

I maded a course where I explain it, but is in spanish.  Bue, read about inlineformset_factory.


Django 2.1 Práctico:   https://goo.gl/2fFjXw
Programación en Capas: https://goo.gl/FHcQpP
Entity FrameWork:      https://goo.gl/L8xych

Mi Blog
Nicaragua

"Si ustedes permanecen unidos a mí, y si permanecen fieles a mis enseñanzas, pidan lo que quieran y se les dará.
(Juan 15:7 DHH)
Bendito el varón que se fía en el SEÑOR, y cuya confianza es el SEÑOR.
(Jeremías 17:7 RV2000)



El mié., 7 nov. 2018 a las 7:06, DELEAU Eric (<eric2lococo@gmail.com>) escribió:
Hello

I'm a new django developper and i must build a statistic web site.
For this, i made some opération.

# models.py
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    categorie = models.CharField(max_length=20)
    pub_creat = models.DateTimeField('date creation')
    # champs à afficher
    def __str__(self):
       return self.question_text

class Response(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    response_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    # champs à afficher
    def __str__(self):
       return self.response_text

views.py
class enfantQueryListView(generic.ListView):
    template_name = 'colorrun/enfant2.html'
    context_object_name = 'liste_question_enfant'
    
    def get_queryset(self):
        return Question.objects.filter(categorie='Enfant')

enfant2.html
{% if liste_question_enfant %}
<ul>
{% for question in liste_question_enfant %}
<li>Question : {{ question.question_text }}</a></li>
    <form action="{% url 'colorrun:vote' question.id %}" method="post">
    {% csrf_token %}
    {% for reponse in question.response_set.all %}
         <input type="radio" name="rep_question" id="rep_question{{ forloop.counter}}" value="{{reponse.id}}">
<label for="rep_question{{ forloop.counter}}"> {{reponse.response_text }}</label><br>
    {% endfor %}
</form>
{% endfor %}
</ul>
{% else %}
<p>Pas de question pour les enfants.</p>
{% endif %}

I hope to save all responses in doer two increment the field "vote" in the table "response".

Actually, i don't kow to implement this mechanism.

Could you help me, I have to shown my first version of the web site on friday in order to validate the POC.

Thanks for your help


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dec9e9d4-18a1-4053-ba35-f818355a1152%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMQeQjbA1CaQxO5%2BuV_1iKyCA8yNzJ-G_KB4Q5-V%2BcnRv1Fn3g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment