【问题标题】:Django form doesn't save value in multiple choice fieldsDjango 表单不保存多项选择字段中的值
【发布时间】:2021-11-16 06:17:24
【问题描述】:

我正在使用 Django 的 ModelForm,但我的表单无缘无故没有保存多项选择字段的值。这是我的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'

还有我的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",)

问题是字段sectionsresponsibles 是外键。当我在表单中选择下拉列表的值之一时,没有问题,并且条目保存时没有明显错误,但表中该字段的值为-。但如果我在管理员中执行此操作,或者我使用 UpdateView 编辑条目,它会起作用。

这昨天有效,我没有改变任何东西......

我的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})

我的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>

【问题讨论】:

  • 我不确定,feedback.capture = capture 是干什么用的? CaptureVersion 没有 capture 字段

标签: django django-forms


【解决方案1】:

在文件'form.html'中你需要放置属性'action'。

-> 'action' 属性告诉表单将​​数据发送到哪里。没有这些信息,什么都不会发生......

你应该这样写:

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

【讨论】:

    【解决方案2】:

    docs中所述:

    仅当您使用 save(commit=False) 时才需要调用 save_m2m()。当您在表单上使用 save() 时,所有数据(包括多对多数据)都会被保存,而无需任何额外的方法调用。

    如果你使用save(commit=False),你必须在之后调用save_m2m(),所以:

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

    【讨论】:

    • 谢谢,现在可以使用了!但是很奇怪,因为它之前没有这个命令就可以工作......有什么解释吗?
    • 嗯不确定,但如果你只使用 formulari_captura.save() 而没有 commit=False 它也可以工作
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2018-09-30
    • 2019-08-05
    • 2011-09-22
    • 2020-05-25
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多