Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#23363 closed New feature (duplicate)

Field length in ORM

Reported by: Gerben Oolbekkink Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: ORM, order_by, QuerySet
Cc: Josh Smeaton Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I can order a query by field length by using .extra('length': 'Length(field)'), but this is a problem when targeting mssql, where the length function is LEN() is it possible to add this to the ORM?

I think it would be a simple addition, and I would be very happy.

Change History (9)

comment:1 Changed 2 years ago by Shai Berger

Resolution: worksforme
Status: newclosed

Django 1.7, due to be released soon, includes a new feature: Custom lookups and transforms.

This allows you to solve the problem yourself.

If this doesn't answer your needs, please bring them up on the DevelopersMailingList.

comment:2 Changed 2 years ago by Josh Smeaton

I don't think custom lookups and transforms works for this use case. The ticket reporter wants to include that information in the select list. Transforms and Lookups are only really used in filter clauses at the moment.

There is a PR that will allow this kind of functionality here: https://github.com/django/django/pull/2496

And to implement the length function, you'd create an object such as:

class Length(Func):
    function = 'LENGTH'

    def as_mssql(self, compiler, query):
        self.function = 'LEN'
        return super(Length, self).as_sql(compiler, query)

    MyModel.objects.annotate(length=Length('myfield'))

I'm reopening on the basis that lookups and transforms don't solve the problem.

comment:3 Changed 2 years ago by Josh Smeaton

Resolution: worksforme
Status: closednew
Version: 1.6master

comment:4 Changed 2 years ago by Aymeric Augustin

Josh, I gather from your comment that this is covered by #14030, isn't it?

If so, this ticket can be closed as a duplicate. If not, could you clarify what needs to be done to close it?

comment:5 Changed 2 years ago by Josh Smeaton

I can't seem to find a ticket requesting an implementation of custom functions (other than extensions to .extra()). #14030 will allow this feature request to be implemented, but the original scope of 14030 was to allow F() objects to be composed with aggregates.

I think we should open a new ticket that defines a list of functions to be implemented by django (pending #14030), and close off any related tickets such as this one as duplicates/wontfixes. I'm happy to open that new ticket, but will have to do so later in the day.

comment:6 Changed 2 years ago by Tim Graham

Cc: Josh Smeaton added

Josh, just wanted to remind you about following up on this if you can.

comment:7 Changed 2 years ago by Josh Smeaton

Thanks Tim. I've resumed the discussion on the mailing list here: https://groups.google.com/forum/#!searchin/django-developers/functions/django-developers/HggiPzwkono/dsnx3BuXpnkJ

Once we reach some sort of consensus or enough time passes without response, I'll go ahead and create the ticket. I'll bookmark this so I remember to come back to it.

comment:8 Changed 2 years ago by Josh Smeaton

Resolution: duplicate
Status: newclosed

comment:9 Changed 2 years ago by Josh Smeaton

#23753 will take over for this ticket.

Note: See TracTickets for help on using tickets.
Back to Top