- Previous thread: How does my Model gets the objects attribute
- Next thread: Django Search Form in Admin
- Threads sorted by date: django 200907
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=E9er de doublons Enseignant (nom,
pr=E9nom, qualit=E9, ...)
# avant d'enregistrer (sur un POST), essayer de rechercher les
enseignants ayant le m=EAme nom, pr=E9nom, qualit=E9
# et de remplacer par l'ID trouv=E9
anneeuniv =3D get_object_or_404(AnneeUniversitaire, annee=3Dannee_id)
ue =3D get_object_or_404(UE, pk=3Due_id)
form1 =3D EnseignantForm()
form2 =3D EnseignantUEForm()
if request.method =3D=3D 'POST':
new_data1 =3D request.POST.copy()
new_data2 =3D request.POST.copy()
# d=E9couper les donn=E9es re=E7ues 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 =3D EnseignantForm(new_data1)
if form1.is_valid():
# recherche de doublons sur les enseignants
# rechercher un enseignant ayant m=EAme nom, pr=E9nom
#doublons =3D Enseignant.objects.filter(nom=3Dnew_data1
['nom']).filter(prenom=3Dnew_data1['prenom'])
# autre solution pour =E9liminer les doublons en conservant
la possibilit=E9 d'annualiser les qualit=E9s, tester sur tous les champs
Enseignant
doublons =3D []
try:
if new_data1['qualite']:
doublons =3D Enseignant.objects.filter(nom=3Dnew_data1
['nom'], prenom=3Dnew_data1['prenom'], qualite__id=3Dint(new_data1
['qualite']), autre_qualite=3Dnew_data1['autre_qualite'], hdr=3Dint
(new_data1['hdr']), retraite=3Dint(new_data1['retraite']))
else:
doublons =3D Enseignant.objects.filter(nom=3Dnew_data1
['nom'], prenom=3Dnew_data1['prenom'], autre_qualite=3Dnew_data1
['autre_qualite'], hdr=3Dint(new_data1['hdr']), retraite=3Dint(new_data1
['retraite']))
except:
pass
if len(doublons) > 0:
# on a trouv=E9 au moins un enseignant ayant m=EAme nom,
pr=E9nom
# prendre le premier de la liste des doublons
enseignant =3D doublons[0]
else:
# pas un doublon
# Pas d'erreurs pour l'enseignant, on peut
l'enregistrer
enseignant =3D form1.save()
# recherche de doublons
# r=E9cup=E9rer ID enseignant cr=E9=E9 ou r=E9cup=E9r=E9 pour l=
e 2e
manipulateur
new_data2['enseignant'] =3D str(enseignant.id)
new_data2['ue'] =3D str(ue.id)
form2 =3D EnseignantUEForm(new_data2)
if form2.is_valid():
enseignantue =3D form2.save()
# rediriger =E0 la page d'=E9dition
return HttpResponseRedirect('/ue/%i/enseignants/' %
ue.id)
form2 =3D 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 =3D EnseignantForm()
form2 =3D EnseignantUEForm()
if request.method =3D=3D 'POST':
new_data1 =3D {}
new_data2 =3D {}
new_data1 =3D request.POST.copy()
new_data2.update(principal=3Dnew_data1.pop('principal', None))
form1 =3D EnseignantForm(new_data1)
if form1.is_valid():
try:
enseignant =3D form1.save()
except IntegrityError:
enseignant =3D Enseignant.objects.get
(prenom__exact=3Dnew_data1['prenom'], nom__exact=3Dnew_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=E9d=E9ric H=E9bert
--~--~---------~--~----~------------~-------~--~----~
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+unsubscribe@goog=
legroups.com
For more options, visit this group at http://groups.google.com/group/django=
-users?hl=3Den
-~----------~----~----~----~------~----~------~--~---
Related Threads
- svn commit: r959868 - /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java - ofbiz
- Argh! Name collision! - python
- Maintain separate i18n translation (.po) for different applications - django
- Other FS support in OpenBSD - openbsd
- grails-user - released OSGi plugin version 0.2 - grails
- Wholesale Sports Shoes Clear Air Force One AAA++quality(www.cnnshoe.com) - python
- Thunderbird RSS: No folder control for retrieving? - thunderbird
- Wine - Re: Spore fails to run on new Wine versions, plus fglrx issues - wine
- emacs in etch vs. unicode - debian
- opensuse - zypper up not showing updates that YaST2 does - opensuse
- Hendrix - Firefox 3.6.8 - firefox
- daily pkgsrc CVS update output - netbsd