Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup models using HStoreManager.from_queryset(HStoreQuerySet)() #127

Open
benoitbryon opened this issue Jul 27, 2015 · 0 comments
Open

Comments

@benoitbryon
Copy link

As explained in https://docs.djangoproject.com/en/1.7/topics/db/managers/#from-queryset, when we have a custom manager and queryset classes, we can use manager's from_queryset() method.

Does this pattern apply here? I mean, in order to setup a model, the documentation currently tells:

class Something(models.Model):
    objects = hstore.HStoreManager()

Shouldn't it be something like this instead?

class Something(models.Model):
    objects = hstore.HStoreManager.from_queryset(hstore.HStoreQuerySet)()

In simplest use case, there is no issue with both setups.
But I encountered issues while using custom queryset and manager classes that inherit from HStoreManager and HStoreQuerySet. I'd like the following code to work:

class CustomQuerySet(hstore.HStoreQuerySet):
    def custom_query(self):
        """A custom method for both manager and queryset."""

class CustomManager(hstore.HStoreManager):
    def custom_manager(self):
        """A custom method for manager only."""

class CustomModel(models.Model):
    objects = CustomManager.from_queryset(CustomQuerySet)()

Is the code above valid?
If it is valid, then I think there is a bug in django-hstore: CustomModel.objects.custom_query raises some 'HStoreQuerySet' object has no attribute 'custom_query' error.

I guess that this is because HStoreManager.get_queryset() returns HStoreQuerySet whereas in my case I want it to return CustomQuerySet. So I had to alter CustomManager like that:

class CustomManager(hstore.HStoreManager):
    def get_queryset(self):
        return CustomQuerySet(self.model, using=self._db, hints=self._hints)

    def custom_manager(self):
        """A custom method for manager only."""

As I understand, things to change would be:

  • HStoreManager doesn't implement custom get_queryset
  • link between HStoreManager and HStoreQuerySet should be setup in model, typically using objects = hstore.HStoreManager.from_queryset(hstore.HStoreQuerySet)() (but could be adapted if users have custom manager and/or custom queryset) => documentation to be updated
  • for backward compatibility, current HStoreManager.get_queryset should be renamed to HStoreManager.get_query_set()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant