首页 > 解决方案 > 如果它们已经存在于数据库中,则选择过滤和删除

问题描述

在回答我之前先看看图片。该 group2 使用按钮保存在 db 中

在此处输入图像描述

表格.py

class EserciziForm(forms.ModelForm):

    class Meta:
        model = models.DatiEsercizi
        exclude = ['gruppo_single']
        #fields = '__all__'
    


class GruppiForm(forms.ModelForm):

    class Meta:
        model = models.DatiGruppi
        exclude = ['gruppi_scheda']

视图.py

def creazione(request, nome):
    scheda = get_object_or_404(Schede, nome_scheda = nome)
    eserciziFormSet = formset_factory(EserciziForm, extra = 0)
    
    if request.method == "POST":
        gruppo_form = GruppiForm(request.POST, prefix = 'gruppo')
        if gruppo_form.is_valid():
            gruppo = gruppo_form.save(commit = False)
            gruppo.gruppi_scheda = scheda
            gruppoName = gruppo_form.cleaned_data['dati_gruppo']
            gruppo.save()

            esercizi_formset = eserciziFormSet(request.POST, prefix='esercizi')
            for esercizi in esercizi_formset:
                esercizi_instance = esercizi.save(commit = False)
                esercizi_instance.gruppo_single = get_object_or_404(DatiGruppi, gruppi_scheda = scheda.id, dati_gruppo = gruppoName)
                esercizi_instance.save()

            return HttpResponseRedirect(request.path_info)

    else:

        gruppo_form = GruppiForm(prefix = 'gruppo')
        esercizi_formset = eserciziFormSet(prefix='esercizi')

    context = {'scheda' : scheda, 'gruppo_form' : gruppo_form, 'esercizi_formset': esercizi_formset}
    return render(request, 'crea/passo2.html', context

模型.py

class DatiGruppi(models.Model):
  giorni_settimana_scelta = [
    ("LUNEDI","Lunedì"),
    ("MARTEDI","Martedì"),
    ("MERCOLEDI","Mercoledì"),
    ("GIOVEDI","Giovedì"),
    ("VENERDI","Venerdì"),
    ("SABATO","Sabato"),
    ("DOMENICA","Domenica")
  ]
  giorni_settimana = MultiSelectField(choices = giorni_settimana_scelta,default = '-')
  dati_gruppo = models.ForeignKey(
    Gruppi,on_delete = models.CASCADE, related_name = 'dati_gruppo')
  gruppi_scheda = models.ForeignKey(Schede,on_delete = models.CASCADE, related_name = 'gruppi_scheda')


class Schede(models.Model):
  nome_scheda = models.CharField(max_length=100)
  data_inizio = models.DateField()
  data_fine = models.DateField()
  utente = models.ForeignKey(User, on_delete = models.CASCADE,related_name = 'utente')
  

标签: djangodjango-modelsdjango-viewsdjango-formsdjango-templates

解决方案


您可以在实例化之前覆盖表单字段,如下所示:

视图.py

from django import forms

if request.method == "POST":
    # Post logic here
else:
    
    # We try to retrieve group that the current user is not yet in.
    # Not your logic, but to sum up, you have to retrieve the groups
    # which had not yet been added.
    # Use a filter that permit you to retrieve only groups which had not yet been added.
    group_to_add = Group.objects.filter(...)
    GruppiForm.base_fields['group_field'] = forms.ModelChoiceField(
        queryset=group_to_add)

# Instantiate the form now
# In this form, the choices are only those contained in the group_to_add queryset
form = GruppiForm(prefix = 'gruppo')

推荐阅读