0

I have a method like this:

def build_query(self, ids):
    return '''select   
        d.name as degree,
        fs.student_id
        from form_status fs
        left join form_fills fc
        on fs.id = fc.form_fills_id
        ...
        where fs.student_id in (*ids);
    '''

and ids is a list that looks like this: [1,2,3,100,10000]

How do I do this properly? I like the triple quoted strings because they allow me to indent the sql string and not look weird. Can I maintain this?

1 Answer 1

2

Since tuples in Python happen to be enclosed in parentheses too, you can convert ids to a tuple in an f-string to make the code look aesthetically to your expectation:

def build_query(self, ids):
    return f'''select   
        d.name as degree,
        fs.student_id
        from form_status fs
        left join form_fills fc
        on fs.id = fc.form_fills_id
        ...
        where fs.student_id in {(*ids,)};
    '''
Sign up to request clarification or add additional context in comments.

2 Comments

Why do I need the trailing comma? What is going on here exactly?
The trailing comma is syntactically required for a tuple with exactly one item. Please see stackoverflow.com/questions/7992559/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.