WayBehind
WayBehind

Reputation: 1697

Django Using Multiple Models with Same Table & Field Names?

I have a Django project where I need to use old database and new database source between pages. They both have same table names and fields names but are loaded with different data.

Now, these are actual models that are already in place loaded with data so it is not like I can start from scratch and design two models with unique names.

The question is. Can I have multiple models with the same table names? How do I distinguish them in my models.py?

Then, how do I tell Django in my view.py which model to use if they have a same table name and same field names? BTW each view will be using one or the other models, so it is not like I need to use both at the same time.

Old DB:

class Business(models.Model):
    name = models.CharField(max_length=50, ...
    city = models.ForeignKey('City', ...
    state = models.CharField( ...
    zipcode = models.CharField(...

New DB:

class Business(models.Model):
    name = models.CharField(max_length=50, ...
    city = models.ForeignKey('City', ...
    state = models.CharField( ...
    zipcode = models.CharField(...

Upvotes: 2

Views: 4433

Answers (1)

Alvaro
Alvaro

Reputation: 12037

Following the discussion, I've come up with this idea:

1) Change your DATABASES setting to point to two different DBs. You can check it here

2) Change your model definition to include both versions and point to the same table. Testing with version 1.7, django will overlook this in model validation, but be very careful with migrations:

class OldBusiness(models.Model):

    class Meta:
        db_table = 'business_business'

    name = models.CharField(max_length=50, ...
    city = models.ForeignKey('City', ...
    state = models.CharField( ...
    zipcode = models.CharField(...


class Business(models.Model):

    class Meta:
        db_table = 'business_business'

    name = models.CharField(max_length=50, ...
    city = models.ForeignKey('City', ...
    state = models.CharField( ...
    zipcode = models.CharField(...

3) Override the object manager to make sure you are using the right db for any query:

class OldManager(models.Manager):

    def get_queryset(self, *args, **kwargs):
        return super(OldManager, self).get_queryset(*args, **kwargs).using('old')


class OldBusiness(models.Model):

    class Meta:
        db_table = 'business_business'

    objects = OldManager()
    name = models.CharField(max_length=50, ...
    city = models.ForeignKey('City', ...
    state = models.CharField( ...
    zipcode = models.CharField(...

That should do it for most practical cases, but if you are updating, you'll have to take other things into account.

As a note: this assumes you left the new database in the "default" key in the DATABASES dict

A very good improvement would be to implement a database router for the OldBusiness model

You could even check for model name and return the old db in that case. Very clean and would work with migrations

Upvotes: 3

Related Questions