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

Implement lookups on values within composite types #9

Closed
danni opened this issue Jun 1, 2016 · 4 comments · Fixed by #32
Closed

Implement lookups on values within composite types #9

danni opened this issue Jun 1, 2016 · 4 comments · Fixed by #32

Comments

@danni
Copy link
Owner

danni commented Jun 1, 2016

It should be possible to do a lookup on a composite type:

Person.objects.filter(address__suburb="Preston")
@raiviskrumins
Copy link

+1

@Dipenbhatt03
Copy link

Hey, is there any active work being done on this, or is there any reference to how one will provide filtering in composite types.

@danni
Copy link
Owner Author

danni commented Nov 13, 2021

No, I've worked on this package on an as-needs basis only. So unfortunately if you want this feature you're going to have to work out how to make it work. It is possible to filter on the whole tuple (e.g. using structs as structured keys).

If you wanted a quick solution, you could write a lookup function (class Lookup/register_lookup) for the generated field type.

You could also consider Postgres' JSON field, the jsonpath support is quite robust now.

OJFord added a commit to OJFord/django-postgres-composite-types that referenced this issue Mar 17, 2023
@OJFord
Copy link
Contributor

OJFord commented Mar 17, 2023

Hi all, feedback welcome on PR linked above if you're still interested in this and have a chance to try it out.

Or, if you prefer, the work-around for a single case I was using prior to working that up to cover all members was:

class SomeType(CompositeType):
    member = # ...
    
SomeField = SomeType.Field

@SomeField.register_lookup
class SomeMemberLookup(django.db.models.Transform):
    lookup_name = "member"
    col_name = next(
        filter(
            lambda af: af[1].attname == "member",
            SomeType._meta.fields,  # pylint: disable=protected-access
        )
    )[1].column

    arity = 1
    template = f'(%(expressions)s)."{col_name}"'

@danni danni closed this as completed in #32 Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants