Прежде всего, вам не хватает :
после после def toto
и def save()
.Отступ также неверен.
Кажется, лучше сначала просто проверить Cours.DoesNotExist
и при необходимости создать его в блоке try.Я не уверен, как работает ваш Кур, но что-то вроде этого:
class toto:
def save():
try:#gestion du cours
try:
cours = Cours.objects.get(titre=self.titre, date=old_date)
except Cours.DoesNotExist as e:
print("trying to create " self.titre)
Cours.objects.create(titre=self.titre, date=self.date, valeur=self.cours)
# I guess you need this again now:
cours = Cours.objects.get(titre=self.titre, date=old_date)
cours.save()
cours.date = self.date
except Cours.DoesNotExist as e:
print("not even at second try: ",e)
raise
except IntegrityError:
pass
except BaseException as e:
print(" re-raise of some exception: ",type(e),e)
raise
Обратите внимание, что порядок, в котором вы ловите исключения, имеет значение, если одно проверенное исключение получено из другого.