Hello, I 'm working on a prexisting application which goal is to manage courses and to link them to categories, teachers and so on...
I have to rewrite a view. In this view, the third of the overall process, users link their course to at least one teacher. The point here is that there is no concept of (teacher) account neither a way to retrieve a prexisting teacher from the DB. For example, lets say that a teacher A has filled in his information for one course, and two weeks after he wants to fill in again information about another course. At this time, there is no way to him to retrieve At this time, in order to avoid duplicate rows in teacher table, a hack has been setted up : (here : Enseignant means Teacher and ue means course , it's in french) ----------------------- CODE -------------------------- @user_passes_test(lambda u: u.has_perm('Baobab_2009.add_ue')) def ue_enseignants(request, annee_id, ue_id): """ Formulaire de saisie des enseignants d'une UE """ # TODO: essayer de ne pas créer de doublons Enseignant (nom, prénom, qualité, ...) # avant d'enregistrer (sur un POST), essayer de rechercher les enseignants ayant le même nom, prénom, qualité # et de remplacer par l'ID trouvé anneeuniv = get_object_or_404(AnneeUniversitaire, annee=annee_id) ue = get_object_or_404(UE, pk=ue_id) form1 = EnseignantForm() form2 = EnseignantUEForm() if request.method == 'POST': new_data1 = request.POST.copy() new_data2 = request.POST.copy() # découper les données reçues avant la validation # une partie pour enseignant (prenom, nom, qualite, autre_qualite, hdr, retraite) # une partie pour enseignantue (principal +ue, +enseignant) if new_data1.has_key('principal'): del new_data1['principal'] if new_data2.has_key('prenom'): del new_data2['prenom'] if new_data2.has_key('nom'): del new_data2['nom'] if new_data2.has_key('qualite'): del new_data2['qualite'] if new_data2.has_key('autre_qualite'): del new_data2['autre_qualite'] if new_data2.has_key('hdr'): del new_data2['hdr'] if new_data2.has_key('retraite'): del new_data2['retraite'] form1 = EnseignantForm(new_data1) if form1.is_valid(): # recherche de doublons sur les enseignants # rechercher un enseignant ayant même nom, prénom #doublons = Enseignant.objects.filter(nom=new_data1 ['nom']).filter(prenom=new_data1['prenom']) # autre solution pour éliminer les doublons en conservant la possibilité d'annualiser les qualités, tester sur tous les champs Enseignant doublons = [] try: if new_data1['qualite']: doublons = Enseignant.objects.filter(nom=new_data1 ['nom'], prenom=new_data1['prenom'], qualite__id=int(new_data1 ['qualite']), autre_qualite=new_data1['autre_qualite'], hdr=int (new_data1['hdr']), retraite=int(new_data1['retraite'])) else: doublons = Enseignant.objects.filter(nom=new_data1 ['nom'], prenom=new_data1['prenom'], autre_qualite=new_data1 ['autre_qualite'], hdr=int(new_data1['hdr']), retraite=int(new_data1 ['retraite'])) except: pass if len(doublons) > 0: # on a trouvé au moins un enseignant ayant même nom, prénom # prendre le premier de la liste des doublons enseignant = doublons[0] else: # pas un doublon # Pas d'erreurs pour l'enseignant, on peut l'enregistrer enseignant = form1.save() # recherche de doublons # récupérer ID enseignant créé ou récupéré pour le 2e manipulateur new_data2['enseignant'] = str(enseignant.id) new_data2['ue'] = str(ue.id) form2 = EnseignantUEForm(new_data2) if form2.is_valid(): enseignantue = form2.save() # rediriger à la page d'édition return HttpResponseRedirect('/ue/%i/enseignants/' % ue.id) form2 = EnseignantUEForm(new_data2) return render_to_response('Baobab_2009/ue_enseignants_form.html', {'form1': form1, 'form2': form2, 'ue': ue, 'anneeuniv': anneeuniv}) --------------------- CODE ----------------------- What I tried to do is a no less stupid hack : put an unique_together constraint on the Enseignant model and catch a hypothetic IntegrityError in the view. With a from django.db import IntegrityError --------------- CODE ----------------------------- form1 = EnseignantForm() form2 = EnseignantUEForm() if request.method == 'POST': new_data1 = {} new_data2 = {} new_data1 = request.POST.copy() new_data2.update(principal=new_data1.pop('principal', None)) form1 = EnseignantForm(new_data1) if form1.is_valid(): try: enseignant = form1.save() except IntegrityError: enseignant = Enseignant.objects.get (prenom__exact=new_data1['prenom'], nom__exact=new_data1['nom']) -------------------------- CODE ------------------------- But it seems that the uniqueness is validate before the call of model.save() and therefore before reaching DB. The form seems never validate even when the teacher did not exist in the DB. My question is : is there a way to catch the duplicate entry exception and to retrieve the previous primary key (is there something like IntegrityError.previous_pk ) Many thanks in advance and apologize for this very long post. Frédéric Hébert --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---