clokep / django-querysetsequence

Chain multiple (disparate) QuerySets in Django
https://django-querysetsequence.readthedocs.io/
ISC License
107 stars 25 forks source link

add query and verbose_name #66

Closed Fethienv closed 4 years ago

Fethienv commented 4 years ago

123

def get_queryset(self):
        db_list = Databases.objects.all().filter(model_name=self.model._meta.model_name).exclude(count=0)
        if db_list.count() != 0:           
            return reduce(QuerySetSequence, [super(CustomModelManager, self).get_queryset().using(db.get_name) for db in db_list])           
        return super(CustomModelManager, self).get_queryset().none()
clokep commented 4 years ago

This doesn't make sense though -- if each of the internal QuerySet instances has a different model we shouldn't arbitrarily choose one to build the query out of.

Fethienv commented 4 years ago

This doesn't make sense though -- if each of the internal QuerySet instances has a different model we shouldn't arbitrarily choose one to build the query out of.

Yes, same models are required, In my case, I used it to get the same table from multidatabases (horizontal scaling) and to avoid that error if we use django-querysetsequence in admin site

Fethienv commented 4 years ago

if each of the internal QuerySet instances has a different model we shouldn't arbitrarily choose one to build the query out of.

We can also add feature return a dictionary if models not the same

clokep commented 4 years ago

In my case, I used it to get the same table from multidatabases (horizontal scaling)

I suspect there's better ways to do this in Django -- maybe using a database router (or using a database proxy or something).

Arbitrarily choosing the first one doesn't make sense. I'd be more inclined to accept it if there's a check if all the models are the same.

Fethienv commented 4 years ago

Thanks, I used django-querysetsequence in this repository https://github.com/Fethienv/django-sharding

clokep commented 4 years ago

I'm going to close this as I think it is the wrong approach. I think I would accept a PR that checks if all the QuerySets have the same underlying model before setting these properties. Also, please include tests! If you're interested in updating this, feel free to shout and I'll re-open it.