Nyssance
Nyssance

Reputation: 341

Django Multi Table/Database queryset

I have two Model

class User(models.Model)
    id = models.IntegerField(primary_key=True)
    username = models.CharField(max_length=20, unique=True)

class Blog(models.Model)
    id = models.BigIntegerField(primary_key=True)
    user_id = models.IntegerField()
    ...

Because User in db User, and Blog in db Blog_1, Blog_2...Blog8, so without ForeignKey

In bloglist views, I want to show blogs with username. How do it with one query? or must have multi queryset? Blog.objects.all() get the user_id, and query every username by user_id ?

And bloglist views show the blogs from the user following, I have a blogid list, but blogs from multi db, so:

for id in blogid_list
    Blog.objects.get(pk=id).using('Blog_%d' % (id % 8))

How combine in one list? I think the code is inefficient, any Suggestions?

Upvotes: 1

Views: 182

Answers (1)

Thomas Schwärzl
Thomas Schwärzl

Reputation: 9917

You need to get the user_id as an integer and then query your blog-database in a second step.

Django does not support cross-database relationships. See the docs here

Upvotes: 2

Related Questions