Opened 10 months ago

Closed 8 months ago

Last modified 8 months ago

#23363 closed New feature (duplicate)

Field length in ORM

Reported by: qurben Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: ORM, order_by, QuerySet
Cc: jarshwah 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 10 months ago by shaib

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

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 10 months ago by jarshwah

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 10 months ago by jarshwah

  • Resolution worksforme deleted
  • Status changed from closed to new
  • Version changed from 1.6 to master

comment:4 Changed 10 months ago by aaugustin

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 10 months ago by jarshwah

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 8 months ago by timgraham

  • Cc jarshwah added

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

comment:7 Changed 8 months ago by jarshwah

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 8 months ago by jarshwah

  • Resolution set to duplicate
  • Status changed from new to closed

comment:9 Changed 8 months ago by jarshwah

#23753 will take over for this ticket.

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