Thursday, December 27, 2018

Re: how can I work with multi model forms

Hi,

Model forms have one model.  To have the form update fields in other models, you can overload form_valid in views so that it writes the addition form field to their respective models.

Ira

Sent from my iPhone

On Dec 26, 2018, at 9:45 AM, Nur Mohsin <follow.mohsin@gmail.com> wrote:

Hi, in views.py form.save() is commented. Remove comment and try again. Then, tell us what happens?

On Wed, Dec 26, 2018 at 6:29 PM Tessnim Zrayga <zrayga.tessnim@gmail.com> wrote:
Hello everyone,
I have this design: there's a firm, a firm can have multiple nodes. Each node can have many scheduled actions. What I'm trying to do is to create a template containing all informations about a firm and be able to create multiple scheduled actions at a time, so this is what the template will look like in the end:

<jhg.png>

Here is what I coded: in models.py:


class Node(models.Model):
ID = models.DecimalField(max_digits=19, decimal_places=10)
name = models.CharField(default='node', max_length=32)
nb_solenoid = models.DecimalField(max_digits=19, decimal_places=10, null=True, blank=True)
connexion = models.CharField(max_length=255)
status = models.BooleanField(default=False)


class Firm(models.Model):

name = models.CharField(max_length=32)
address = models.CharField(max_length=32)


class ScheduledAction(models.Model):
date = models.DateTimeField(default=datetime.now, blank=True)
firm = models.ForeignKey('Firm', on_delete=models.CASCADE, null=True, blank=True)
node = models.ForeignKey('Node', on_delete=models.CASCADE, null=True, blank=True)

views.py:
def planification_view(request, id):
obj = Firm.objects.get(id=id)
form = ScheduledActionForm(request.POST or None)
form1 = ScheduledActionForm(request.POST or None)
form2 = FirmForm(request.POST or None)
if form1.is_valid() and form2.is_valid():
#form.save()
print(type(form))
print(form)
context = {
'object': obj,
'form': form
}
return render(request, "node/planification.html", context)

planification.html:
<script>
function submitForms(){
document.forms["form1"].submit();
document.forms["form2"].submit();
}
</script>
<div name="FirmName">
{{ object }}
<h1>Firm: {{ object.name }}</h1>
</div>
<div name="NodesOfFirm">
<form method="POST" action="." name="form1">{% csrf_token %}
{% for instance in object.node_set.all %}
<input type="checkbox" name="{{ instance.name }}" value="{{ instance.id }}"> {{ instance.name }}<br>
{% endfor %}
</form>
</div>
<div name="planification">
<h1>Planification</h1>
<form method="POST" action="." name="form2">{% csrf_token %}
{{ form.as_p }}
</form>
<input type="button" value="Click Me!" onclick="submitForms()" />
</div>
<div name="scheduledActions">
<h1>Scheduled Actions</h1>
{% for instance in object.scheduledaction_set.all %}
<p>{{instance.date}} - {{ instance.node }}</p>
{% endfor %}
</div>

But I can't go further and this code doesn't seem to work. I can check nodes and click submit but nothing is stored in the database.



--
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/5d2fd9de-991e-4526-8303-84287df0dfc2%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/CAM0c9RqU7VW0B-BaR_O-ahqOTZEb9AXhc_ZUSaCd%2Bckqr3M9og%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment