Home > Back-end >  Django form doesn't save value in multiple choice fields
Django form doesn't save value in multiple choice fields

Time:09-23

I'm using Django's ModelForm and for no reason my form doesn't save the value of multiple choice fields. This is my models.py:

class CaptureVersion(models.Model):
    id_capture = models.AutoField(primary_key=True)
    name = models.CharField(unique=True, max_length=50, verbose_name="Nom Disseny")
    int_reference_number = models.CharField(max_length=15, unique=True, blank=True, null=True, verbose_name='IRN')
    capture_version_id_provider = models.ForeignKey(Provider, on_delete=models.CASCADE, db_column='id_provider', verbose_name='Proveïdor')
    associated_register = models.CharField(max_length=150, blank=True, null=True, verbose_name='Registre associat')
    start_date = models.DateField(blank=True, null=True, verbose_name='Data Alta')
    end_date = models.DateField(blank=True, null=True, verbose_name='Data Baixa')
    target_size = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True, verbose_name='Regions diana (Mb)')
    probe_size = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True, verbose_name='Sondes (Mb)')
    sections = models.ManyToManyField(Section, blank=True, verbose_name="Secció")
    responsibles = models.ManyToManyField(Responsible, blank=True, verbose_name="Responsable/s")

    class Meta:
        ordering = ['start_date']
        db_table = 'capture_version'

And my forms.py:

class FormulariCaptura(ModelForm):
    class Meta:
        model = CaptureVersion
        fields = ("name", "int_reference_number", "capture_version_id_provider", "associated_register", "start_date", 
        "end_date", "target_size", "probe_size", "sections", "responsibles",)

The problem are the fields sections and responsibles which are foreign keys. When I select one of the values of the dropdown list in the form, there's no problem and the entry is saved with no apparent errors but the value in the table for that field is -. But if I do it in the admin, it works or if I edit the entry with UpdateView.

This worked yesterday and I didn't change anything...

My views.py:

def capture_form(request):

   formulari_captura=FormulariCaptura()

   if request.method=="POST":

      formulari_captura=FormulariCaptura(request.POST or None)

      if formulari_captura.is_valid():

        feedback = formulari_captura.save(commit=False)
        capture = CaptureVersion.objects.all()
        feedback.capture = capture
        feedback.save()
        messages.success(request, 'Kit de captura enregistrat correctament!')        

    return render(request, "captureVersion/formulari_captura.html", {'formulari':formulari_captura})

My form.html:

<div class="formMostra">

{% if messages %}
    {% for message in messages %}
    <p style="margin: 20px;" {% if message.tags %} class=" {{ message.tags }} " {% endif %}> {{ message }} </p>
    {% endfor %}
{% endif %}

<form action="" method="POST" style="text-align: justify;">
    {% csrf_token %}
   <table style="margin: 20px;">{{formulari.as_table}}</table>
   <input type="submit" value="Afegir" style="width: 100px; margin: 20px; margin-right: 0px;" class="btn btn-primary">
   <input onclick="location.href='/sample/'" value="Cancel·lar" style="width: 100px; margin: 20px; margin-left: 0px;" class="btn btn-light">
</form>

CodePudding user response:

In the file 'form.html' you need to put the attribute 'action'.

-> The 'action' attribute tells the form where to send the data. Without that information nothing happens ...

You should put something like:

<form action="{% url 'test_url' %}" method="POST">

CodePudding user response:

As described in the docs:

Calling save_m2m() is only required if you use save(commit=False). When you use a save() on a form, all data – including many-to-many data – is saved without the need for any additional method calls.

If you use save(commit=False), you have to call save_m2m() after, so:

feedback = formulari_captura.save(commit=False)
feedback.save()
formulari_captura.save_m2m()
  • Related