That sounds more like an exact use case for model inheritance. You would define a single model that matches the columns in both tables. You can define that model as abstract, then have the two real models (one for each table) inherit from that abstract model. In the definition for the child models, you would specify the DB table names to match your two tables in the existing schema.
https://docs.djangoproject.com/en/1.7/ref/models/options/#db-table
That way, you only maintain a single (abstract) model definition, even though multiple models use it (each with a separate DB table).
You may also need to mark the real models as unmanaged by the ORM if you're doing funny things directly in the DB, and you will be managing the table schema directly for those tables.
https://docs.djangoproject.com/en/1.7/ref/models/options/#managed
I'm assuming you have already determined the logic deciding which model each user uses. At that point, assuming that all other functionality remains the same (calls/processing the data, regardless of table), the calls to those models can be generalized by a simple utility function that returns a class instance of the necessary model.
def get_right_model(user):
# logic to pick ModelA or ModelB based on user
return ModelA
Then later, maybe in a view:
selected_model = get_right_model(self.request.user)
all_data = selected_model.objects.all()
#all_data should now contain the results of the query against the right model/table in the DB
Trying to flip between tables in the ORM by tweaking the innards probably is going to lead to excessive complication.
-James
On Mar 17, 2015, at 3:08 AM, James Schneider <jrschn...@gmail.com> wrote:
That sounds more like an exact use case for model inheritance. You would define a single model that matches the columns in both tables. You can define that model as abstract, then have the two real models (one for each table) inherit from that abstract model. In the definition for the child models, you would specify the DB table names to match your two tables in the existing schema.
https://docs.djangoproject.com/en/1.7/ref/models/options/#db-table
That way, you only maintain a single (abstract) model definition, even though multiple models use it (each with a separate DB table).
You may also need to mark the real models as unmanaged by the ORM if you're doing funny things directly in the DB, and you will be managing the table schema directly for those tables.
https://docs.djangoproject.com/en/1.7/ref/models/options/#managed
I'm assuming you have already determined the logic deciding which model each user uses. At that point, assuming that all other functionality remains the same (calls/processing the data, regardless of table), the calls to those models can be generalized by a simple utility function that returns a class instance of the necessary model.
def get_right_model(user):
# logic to pick ModelA or ModelB based on user
return ModelAThen later, maybe in a view:
selected_model = get_right_model(self.request.user)
all_data = selected_model.objects.all()
#all_data should now contain the results of the query against the right model/table in the DB
Trying to flip between tables in the ORM by tweaking the innards probably is going to lead to excessive complication.
-James
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXKkQ2Lpq1F-KBpHkndLWxi_8dCVamXJ-G7Bji3QZWcmA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0435A65C-4A7A-4DFA-A1C2-8951437FFB2B%40Radio1190.org.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUcS608B7QPza2adH7KQ5pBjozzMRnW8Am8yY7SSN_UqA%40mail.gmail.com.