Skip to content

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

Open
@benoitbryon

Description

@benoitbryon

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions