Jaberwocky Вот мой код, я должен заранее извиниться за мой код, который я все еще изучаю
class Ubc(models.Model):
""" Table Ubc """
name = models.CharField(verbose_name="Nome da instituição", max_length=255, null=False)
laboratory_departament = models.CharField(max_length=255, null=True, blank=True, verbose_name="Laboratório")
cep = models.CharField(max_length=255, verbose_name="Cep", null=True)
cnpj = models.CharField(max_length=255, verbose_name="CNPJ", null=True)
lat = models.FloatField(blank=True, null=True)
lng = models.FloatField(blank=True, null=True)
street = models.CharField(max_length=255, verbose_name="Endereco", null=True, blank=True)
uf = models.CharField(max_length=255, verbose_name="UF", null=True, blank=True)
city = models.CharField(max_length=255, verbose_name="City", null=True, blank=True)
parent_institution_name = models.CharField(verbose_name="Nome da instituição vinculada", max_length=255, blank=True, null=True)
coordinator = models.CharField(max_length=255, verbose_name="Pessoa Responsavel")
email = models.CharField(max_length=255, verbose_name="E-mail")
website = models.CharField(max_length=255, blank=True, null=True, verbose_name="Website")
rad_operating_time = models.IntegerField(verbose_name="Tempo de atuação em projetos de P&D+I")
phone = models.CharField(max_length=255, verbose_name="Telefone", null=True)
inactive = models.NullBooleanField(verbose_name="Inativo", default=False, null=True)
partner_category = models.ForeignKey('PartnerSubCategory', on_delete=models.CASCADE)
parent = models.ForeignKey('Company', blank=True, null=True, on_delete=models.CASCADE)
general_grade = models.ForeignKey('GeneralGrade', blank=True, null=True, on_delete=models.CASCADE)
main_product = models.ManyToManyField('MainProductUbc', verbose_name="Qual o tipo de produto e/ou servico que a UBC fornece (produto, processo, mudanca organizacional)")
tokens = models.ManyToManyField('ubc.Token', blank=True, related_name='tokens')
activity_branch = models.ForeignKey('ActivityBranch', verbose_name="Ramo de Atividade (CNAE)", null=True, on_delete=models.CASCADE)
researchers = models.ManyToManyField('researcher.Researcher', related_name='researchers', through='researcher.ResearcherUbc', blank=True)
research_line = models.ManyToManyField('researcher.ResearchLines',related_name='ubc_lines', blank=True)
class ResearchLines(models.Model):
title = models.CharField(verbose_name="Descrição da Linha de Pesquisa", max_length=255, blank=True, null=True)
ubc = models.ForeignKey('ubc.Ubc', on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updtaed = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = 'Research Line'
def __str__(self):
return self.title
views.py
def ubc_register(request):
if request.method == "POST":
form = UbcForm(request.POST or None)
if form.is_valid():
form = form.save(commit=False)
formset = LinesFormSet(request.POST, request.FILES, instance=form)
if formset.is_valid():
form.save()
formset.save()
return HttpResponseRedirect(reverse('index'))
else:
form = UbcForm(prefix="form")
formset = LinesFormSet(prefix="formset")
context = {
'form':form,
'formset':formset
}
return render(request, 'accounts/ubc_form.html', context)
forms.py
class UbcForm(forms.ModelForm):
class Meta:
research_line = forms.ModelMultipleChoiceField(queryset=ResearchLines.objects.all())
model = Ubc
exclude = ('employee_number', ' revenues', 'filter_grade', 'grade',
' knowledge_grade',
'application_grade', 'ie_grade', ' ia_grade', 'final_grade',
'inactive', 'last_coordinator_access', ' hr_count',
'hr_returned', ' match_hr_count', 'general_grade', 'lat', 'lng'
'tokens', 'iso_certification', 'other_certification', 'researchers',
'thematic_network',
)
widgets ={
'name':forms.TextInput(attrs={
'class':'form-control',
'placeholder': 'Nome da UBC'
}),
'parent_institution_name': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'Nome da Instituição à qual a UBC é vinculada'
}),
'laboratory_departament': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'Laboratório/Departamento'
}),
'cnpj': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'CNPJ'
}),
'institution_name': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'Nome da instituição'
}),
'coordinator': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'Pessoa Responsável'
}),
'email': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'E-mail'
}),
'website': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'Website'
}),
'rad_operating_time': forms.NumberInput(attrs={
'class':'form-control',
'placeholder':'Tempo de atuação em projetos de P&D+I'
}),
'phone': forms.TextInput(attrs={
'class':'form-control',
'placeholder': 'Telefone'
}),
'partner_category': forms.RadioSelect(),
'main_product':forms.CheckboxSelectMultiple( attrs={
'type':'checkbox'
}),
'activity_branch':forms.Select( attrs={
'class':'form-control'
}),
'research_line':forms.TextInput(attrs={
'class':'form-control'
}),
'cep': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'CEP'
}),
'street': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'Endereço'
}),
'city': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'Cidade'
}),
'uf': forms.TextInput(attrs={
'class':'form-control',
'placeholder':'UF'
}),
}
class ResearchLineForm(forms.ModelForm):
class Meta:
model = ResearchLines
fields = ('title', )
widgets = {
'title':forms.TextInput(attrs={
'class':'form-control'
})
}
class AddressForm(forms.ModelForm):
model = Address
fields = '__all__'
LinesFormSet = inlineformset_factory(
Ubc,
ResearchLines,
fields=('title', ),
extra=1,
widgets = {
'title':forms.TextInput(attrs={
'class':'form-control'
})
}
)
внутри моего form.html тега formset и сценария из django-dynamic-formset для создания новой формы
{{formset.management_form}}
{% for form in formset %}
<div class="link-formset">
{{ form }}
</div>
{% endfor %}
<script>
$('.link-formset').formset({
addText: 'add link',
deleteText: 'remove'
});
</script>