I am using the postgres unaccent
feature for a django project.
This makes my app database specific and I want to be able to use some others databases (postgres without with extension or others). In this case of course, I don't use unaccent
I would like to have something transparent for the user. I image my code should look like something like:
def get_objects(text):
try:
qs = MyModel.objects.extra(
where=[u"UPPER(unaccent("name")) LIKE UPPER(unaccent(%s))"],
params = [u"%{0}%".format(text)]
)
return list(qs)
except DatabaseError, msg:
qs = MyModel.objects.filter(name__icontains=text)
return list(qs)
Unfortunately, if unaccent
is not installed on the database, the DatabaseError is raised but the 2nd query fails with the following error:
DatabaseError: current transaction is aborted, commands ignored until end of transaction block
I've tried to add transaction support and to rollback it without any success.
What is the best way to manage this error and make the code working whether unaccent
is available or not.