W1ldPo1nter / django-queryable-properties

Write Django model properties that can be used in database queries.
BSD 3-Clause "New" or "Revised" License
72 stars 1 forks source link

[Thought] Support propertiers that accept arguments (methods) #6

Closed SafaAlfulaij closed 1 year ago

SafaAlfulaij commented 3 years ago

Sometimes I find myself needing to pass the user to the property to filter based on his access, etc. I know that properties doesn't accept arguments. Django provides methods to models and querysets. What if we can have the same with properties to methods?

W1ldPo1nter commented 3 years ago

Well, first of all I would have to rename the project as properties don't take parameters by definition. 😄

Seriously though, I don't really know what that would actually look like. The whole extension is built around the idea of using properties the same way as Django's fields - on both the object level as well as the queryset level. This only really works because both fields and properties cannot be accessed with arguments.

For example, both fields and queryable properties can be used to filter a queryset like

MyModel.objects.filter(my_property='some_value')

I'm not sure how to get arguments for the property in there in a way that's convenient and intuitive. If you have any suggestions, let me know.

SafaAlfulaij commented 3 years ago

:)

I forgot to mention that what I'm thinking about is annotations only, and not filterable or updatable properties, as it might not make sense there.

W1ldPo1nter commented 3 years ago

The thing is, those features aren't completely unrelated - an annotatable property can be used for filtering implicitly. Completely separating annotations from filters would essentially remove features and make writing properties harder, which is why I wouldn't really want to do that. Also, queryable properties are currently designed to be usable in all scenarios (depending on what exactly gets implemented with a specific property, of course) and IMO it would be way cleaner to keep this design philosophy and not have properties that can only be used in certain circumstances.

So for me, the problem described above would have to be solved in order to implement something like this properly.