How to make queries from one django project to another

Hi, I have two different django projects one is a blog and the other is a portfolio. I want to query a few blog posts in the portfolio project. If the two projects are hosted from different hosts and use separate PostgreSQL in another host (or might be as docker services), how to make the queries? I understand both projects can connect to multiple databases by the proper configuration, but as there are no python models for the blog in the portfolio projects, how the post should be queried in the portfolio project?

You might have to generate a csv file from one project, and query the csv file from the other one.

https://docs.djangoproject.com/en/4.1/howto/outputting-csv/

I would probably look up tutorials on this, because it might be tricky with data serialization and all that. I didn’t do it before in Django.

edit -

If you’re using the same postgres database in both projects, why not just configure the same database credentials in settings.py on both projects, and query the database as normal?

You have a couple different options here:

  • Create an api in your blog project to search-and-return results

  • Create a secondary database connection to the blog database and either:

    • Add the blog models to your project, defining managed = False
    • Use raw sql to query those tables

Things can get even more sophisticated than that if you need them to be, but it doesn’t sound like it yet from what you describe so far.

In brief, the followings are the relevant files and code:

blog/Blog/models.py

class Post(models.Model):
    title = models.CharField(max_length=100)
    overview = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)
    content = QuillField()
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    thumbnail = models.ImageField()
    categories = models.ManyToManyField(Category)

portfolio/Portfolio/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'blog': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_NAME'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': os.environ.get('HOST'),
        'PORT': os.environ.get('PORT'),
    }
}

portfolio/home/views.py

# What will be my code here to query the post?

Thanks @KenWhitesell. I was able to connect to the blog database. But regarding the next thing, did you roughly mean copying the class Post(models.Model) from the blog’s models.py to the portfolio models.py, and then query using that class? I wanted to avoid rewriting the class Post(models.Model) into portfolio models.py because it needs to revised every time if there is any changes to my base blog’s models.py. Even it was complaining about different db table names as they were in different app (folder) so the prefix was different. Was there any way to sync or share the blog app in-between both the blog and portfolio projects? I am thinking this might help to avoid rewriting code. Regarding raw sql to query, is it possible to use it without python model? Additionally, I am not sure how to do it with different filed like QuillField().

I have added more details in the reply. Please advise me what could be the potential solutions preferably with draft code.

Thanks @Spha. I was just wondering if there was any other convenient ways.

You should mention what views you’re trying to use, class based views/function based views?

You also haven’t discussed what you intend to do with the blog posts after querying them. Why are you querying them?

The same as any other query for any other model. See Multiple databases | Django documentation | Django for the details on querying alternate databases.