How to filter between two databases?

66 Views Asked by At

I have two databases:

   DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'new',
        'USER': 'xxxxx',
        'PASSWORD': 'xxxxx',
        'HOST': '',
        'PORT': '',
    },
    'old': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'old',
        'USER': 'xxxxx',
        'PASSWORD': 'xxxxx',
        'HOST': '',
        'PORT': '',
    },
}

and model:

class MyModel(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=200)
    text = models.TextField()
    data = some other data

Old database contains some removed entries and some of the new database.How to filter entries from new database that there are no entries from the old database?

1

There are 1 best solutions below

0
On

I made this:

f1 = MyModel.objects.using('old').all()

pids = [p.id for p in f1]

f2 = MyModel.objects.exclude(id__in=pids).filter()

But I don't know if it is good.