Hi,
I've post my issue on Stackoverflow and do not have any ansewer so far.
Hope some Django dev could me here ;)
I try using FBV (logic more easy to understand for me).
Maybe I should have used inlineformset_facory to deal with FK but for
conception concerns patient is passed in context.
I faced many issues I've tried to solved but it is more "hack" and I would
like to find a proper way to do the same:
1.
empty_forms was submitted and create a record in database; normally,
default formset behavior is not to submit empty formset I manage this case
using condition form.cleaned_data != {}. It is maybe cause
2.
I define unique_together in my model Traitement. But it seems this is
not manage by Django in form validation. When submitting a form with
duplicates, IntegrityError is raised. I manage this case using a try-catch
bloc. I also tried to define a form validation with clean() method in
TraitementForm but doesn't seems to works.
3.
With traitement_create() code below, all errors can not be displayed at
the same time: forma validation will be displayed but not the
IntegretyError if any.
I have read many documentation and tutorial but could not find answer.
Don't know how I can post code here so I attached txt doc with part of my
code.
Jérôme
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/1245e851-ca03-4ac4-b6e6-7863eb4b2929n%40googlegroups.com.
#models.py
class Patient(models.Model):
ide = models.AutoField(primary_key=True)
pat = models.CharField("Patient",max_length=5, unique=True, null=True,
blank=True, error_messages={'unique':'Un patient avec ce code existe déjà'})
class Traitement(models.Model):
ide = models.AutoField(primary_key=True)
pat = models.ForeignKey('Patient',on_delete = models.CASCADE,
related_name='traitement_patient', db_column='pat')
arv_num = models.IntegerField('Réf/code', null=True, blank=True)
# views.py
def traitements_create(request, pk):
template_name = 'ecrf/traitements_form.html'
patient = Patient.objects.get(ide=pk)
if request.method == 'POST':
formset = TraitementFormSet(request.POST or None,
queryset=Traitement.objects.filter(pat=pk))
if formset.is_valid():
error_occurred = False
for form in formset:
if form.is_valid() and form.cleaned_data != {}:
try:
traitement = form.save(commit=False)
traitement.pat = patient
traitement.arv_sai_log = request.user.username
traitement.save()
except IntegrityError:
form.add_error('arv_num', 'Une fiche Traitement ARV
existe déjà pour ce patient avec ce numéro')
error_occurred = True
break
if not error_occurred:
return redirect('ecrf:patient_list')
else:
formset = TraitementFormSet(queryset=Traitement.objects.filter(pat=pk))
return render(request, template_name, {'formset': formset, 'patient':
patient})
# forms.py
class TraitementForm(forms.ModelForm):
ide = forms.IntegerField()
arv_num = forms.IntegerField(label =
'Réf/code',widget=forms.TextInput(attrs={'class': 'form-control','placeholder':
'','data-mask':'00'}))
arv_deb_dat = forms.DateField(label = 'Date de début',
widget=forms.DateInput(attrs={'class': 'form-control datepicker2',
'autocomplete': 'off', 'placeholder': '', 'data-date-format': 'dd/mm/yyyy',
'data-theme':'dark'}, format='%d/%m/%Y')
,input_formats=settings.DATE_INPUT_FORMATS,required=False)
class Meta:
model = Traitement
fields = ['ide','arv_num','arv_deb_dat',...,]
def clean_arv_deb_dat(self):
data = self.cleaned_data['arv_deb_dat']
if data:
entrydate = datetime.datetime.strptime(str(data), "%Y-%m-%d")
currentdate = datetime.datetime.now()
if entrydate > currentdate:
raise forms.ValidationError("Merci de vérifier la date")
return data
TraitementFormSet = modelformset_factory(
Traitement,
form=TraitementForm,
extra=1,
)