【问题标题】:select filtering and removal if they are already present in the db如果它们已经存在于数据库中,则选择过滤和删除
【发布时间】:2021-12-02 16:55:56
【问题描述】:

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

form.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']

views.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

models.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')
  

【问题讨论】:

  • 您使用什么视图王来渲染模态?向我们展示views.pyform.py 文件

标签: django django-models django-views django-forms django-templates


【解决方案1】:

您可以在实例化之前覆盖表单字段:

views.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')

【讨论】:

  • 我输入了视图和表单代码
  • 这个code一定要放在get request中吧?
  • 是的,我正在更新答案
  • 抱歉,该代码没有意义。用户在该选项卡中创建一个选项卡我可以添加组为什么我必须按用户 ID 过滤?
  • 您要保留的是queryset 属性应该只是尚未添加的组。 前: all_groups - already_added_groups !你明白吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 2021-07-24
  • 2020-02-08
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多