Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why I Hate the Django ORM

Why I Hate the Django ORM

Alex Gaynor

January 12, 2012
Tweet

More Decks by Alex Gaynor

Other Decks in Programming

Transcript

  1. WHY I HATE THE
    DJANGO ORM
    Thursday, January 12, 2012

    View full-size slide

  2. HI, I’M ALEX
    I HATE THE DJANGO ORM
    Thursday, January 12, 2012

    View full-size slide

  3. I ALSO HELP BUILD IT
    (SO ALL OF THIS STUFF IS
    REALLY MY FAULT)
    Thursday, January 12, 2012

    View full-size slide

  4. WHY DO I
    HATE IT?
    Thursday, January 12, 2012

    View full-size slide

  5. SELECT
    event_type
    SUM(end_time - start_time)
    FROM
    event
    GROUP BY
    event_type
    Thursday, January 12, 2012

    View full-size slide

  6. TAKING A STEP BACK
    Thursday, January 12, 2012

    View full-size slide

  7. WHY DO WE HAVE AN ORM?
    DATABASE RECORDS INTO OBJECTS
    EXECUTE COMMON QUERIES
    NOT REPLACE SQL
    Thursday, January 12, 2012

    View full-size slide

  8. WAS THAT A
    COMMON QUERY?
    Thursday, January 12, 2012

    View full-size slide

  9. IT HAS A SYNTAX
    Event.objects.values(
    “event_type”
    ).annotate(
    Sum(F(“end_time”) - F(“start_time”))
    )
    Thursday, January 12, 2012

    View full-size slide

  10. Traceback (most recent call last):
    File "", line 1, in
    File ".../query.py", line 741, in annotate
    is_summary=False)
    File ".../sql/query.py", line 990, in add_aggregate
    field_list = aggregate.lookup.split(LOOKUP_SEP)
    AttributeError: 'ExpressionNode' object has no attribute 'split'
    FFFFFFFUUUUUUUUUUUU
    Thursday, January 12, 2012

    View full-size slide

  11. AGGREGATES TAKE STRINGS
    (NOT EXPRESSIONS OR
    ANYTHING ELSE USEFUL)
    Thursday, January 12, 2012

    View full-size slide

  12. NO COMPOSABILITY
    Thursday, January 12, 2012

    View full-size slide

  13. WEREN’T DESIGNED WITH
    EACH OTHER IN MIND
    MUCH LESS A
    COMPREHENSIVE SYSTEM
    Thursday, January 12, 2012

    View full-size slide

  14. SQLALCHEMY
    session.query(
    TimlineEvent.event_type,
    func.sum(TimelineEvent.end_time - TimelineEvent.start_time)
    ).group_by(
    TimelineEvent.event_type
    ).all()
    Thursday, January 12, 2012

    View full-size slide

  15. NOTICE THAT?
    SQLALCHEMY USES THE SAME SYNTAX
    FOR FIELDS, EVERYWHERE
    Thursday, January 12, 2012

    View full-size slide

  16. DJANGO DOESN’T
    F(“field_name”)
    Aggregate(“field_name”)
    Q(field_name=value)
    filter(field_name=value)
    Thursday, January 12, 2012

    View full-size slide

  17. STRINGS, KEYWORD
    ARGUMENTS, AND FUNKY
    ONE-LETTER OBJECTS
    (WITH VARYING LEVELS OF
    INTEROPERABILITY)
    Thursday, January 12, 2012

    View full-size slide

  18. WHEN DO YOU USE WHICH?
    I HAVE NO IDEA
    (AND I WROTE THE DAMNED THING)
    Thursday, January 12, 2012

    View full-size slide

  19. SIMPLE THINGS ARE SIMPLE
    MEDIUM THINGS ARE POSSIBLE
    HARD THINGS AREN’T
    Thursday, January 12, 2012

    View full-size slide

  20. SIMPLE THINGS SHOULD BE SIMPLE
    HARD THINGS SHOULD BE POSSIBLE
    Thursday, January 12, 2012

    View full-size slide

  21. SPEAKING OF SIMPLE...
    Event.objects.filter(
    start_date__hour=10
    )
    Thursday, January 12, 2012

    View full-size slide

  22. AND OUGHT TO BE SIMPLE
    Event.objects.filter(
    start_date__hour__gt=11
    )
    Thursday, January 12, 2012

    View full-size slide

  23. __ SYNTAX IS NEAT
    BUT LIMITED
    Thursday, January 12, 2012

    View full-size slide

  24. THE IDEALS OF THE ORM ARE
    GOOD
    THE API IS GREAT FOR SIMPLE
    CASES
    Thursday, January 12, 2012

    View full-size slide

  25. BUT THE INCONSISTENCIES
    BLEED
    AND THINGS THAT SHOULD BE
    EASY AREN’T
    Thursday, January 12, 2012

    View full-size slide

  26. 80/20 SOLUTION
    BUT IT COULD BE 90/10
    Thursday, January 12, 2012

    View full-size slide

  27. THANKS
    QUESTIONS?
    Thursday, January 12, 2012

    View full-size slide