Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
ADD multiple objects with form

I'm trying to do something which I would think is quite common, but I'm just really unsure how to realise it.

I have modal 'Requirement'. Every requirement has there own name and unique code. I want to show form with REQUIREMENTS_CHOICES list with checkboxs. User can select checkboxes which requirements they need. How to make it in Django?

enter image description here

models.py:

class Requirement(models.Model):
     code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)
     symbol = models.CharField(_('Symbol'), max_length=250)
     name = models.CharField(_('Name'), max_length=250)

forms.py:

class RequirementAddForm(forms.ModelForm):
    name = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, choices=REQUIREMENTS_CHOICES,)

    class Meta:
        model = Requirement
        fields = ('symbol',)

 REQUIREMENTS_CHOICES = (
     ('A', 'Name A'),
     ('B', 'Name B'),
     ('C', 'Name C'),
 )

views.py:

def requirement_add(request, project_code):
data = dict()
if request.method == 'POST':
    form = RequirementAddForm(request.POST)
    if form.is_valid():
        symbols = form.cleaned_data.get('symbol') # List of symbols which user selected ['A', 'C']
        for symbol in symbols:
            requirement = form.save(commit=False)
            requirement.symbol = symbol
            requirement.name = symbol.get_symbol_display() #???
            requirement.save()
        data['form_is_valid'] = True
        requirements = Requirement.objects.filter()
        context = {'requirement': requirement, 'requirements': requirements}
        data['html_requirement'] = render_to_string('project/requirement_list.html', context)
    else:
        data['form_is_valid'] = False
else:
    form = GroupRequirementAddForm()
context = {'form': form}
data['html_requirement_form'] = render_to_string('project/requirement_add.html', context, request=request)
return JsonResponse(data)
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda