django - How to make exception for oracle backend in custom field?

170 Views Asked by At

THE PROBLEM: I'm writing custom django model field. In get_db_prep_save(self, value, connection) I need to do exception for oracle backend and behave differently from other backends. So I'm looking for the best, official and most elegant way of doing this.

DISCLAIMER: I won't accept answers saying: "use settings.DATABASES and look for django.db.backends.oracle" because it's simply wrong. Using settings I can't figure out which database I'm using currently (default or some other), oracle backend path may change in future from django.db.backends.oracle to something else. What I'm looking for is the best method to check if I'm dealing with oracle when saving custom field to the database.

2

There are 2 best solutions below

0
On BEST ANSWER

I hate answering my own questions but I figured out that the correct answer is:

connection.vendor
0
On

Looking at the docs

The specific connection that will be used for the query is passed as the connection parameter. This allows you to use backend-specific conversion logic if it is required.

So the connection parameter could give you the infos on what backend you are going to connect. The connection parameter should be a proxy object (look at this code ) for the actual DB_ALIAS that is going to perform the save.