def save(self): """Sauvegarde en BDD les colleurs/users du formulaire""" # on ne peut pas utiliser bulk_create ici, puisqu'on a besoin des pk pour les relation un-un et many-many for form in self.forms: colleur= Colleur(grade=form.cleaned_data['grade'],etablissement=form.cleaned_data['etablissement']) colleur.save() # on sauvegarde en BDD pour avoir un pk, indispensable pour les relations many-many # on ajoute les matières et les classes colleur.matieres=form.cleaned_data['matiere'] colleur.classes=form.cleaned_data['classe'] # on crée le user user = User(username=random_string(),first_name=form.cleaned_data['prenom'].lower(),last_name=form.cleaned_data['nom'].lower(),email=form.cleaned_data['email'],colleur=colleur) user.set_password(form.cleaned_data['motdepasse']) user.save()
def save(self): with transaction.atomic(): for form in self.forms: user = User(first_name=form.cleaned_data['first_name'].lower(), last_name=form.cleaned_data['last_name'].lower(), email=form.cleaned_data['email'], username=form.cleaned_data['username']) user.set_password(form.cleaned_data['password']) eleve = Eleve(classe=form.cleaned_data['classe'], photo=form.cleaned_data['photo'], ddn=form.cleaned_data['ddn'], ldn=form.cleaned_data['ldn'], ine=form.cleaned_data['ine'], lv1=form.cleaned_data['lv1'], lv2=form.cleaned_data['lv2']) eleve.save() user.eleve = eleve user.save()