is there a way to add in more dict after inside an append? django

Viewed 114

Apologize if the title is a bit hard to understand. Not too sure how to explain it within a line.

What I want to do is...

actually I have this code

    output = []

    objs = Model.objects.filter(abc=abc) # this would return a some queryset
    for obj in objs:
        output.append({
            TITLE: obj.title,
            TYPE: obj.type,
            # I want to add something here which is kind of like, if `obj.type` is 'hello' then print obj.word
        })

    return output

for example, if my obj.type is 'hello'

I want

        output.append({
            TITLE: obj.title,
            TYPE: obj.type,
            WORD: obj.word,
        })

if my obj.type is 'world'

I want

        output.append({
            TITLE: obj.title,
            TYPE: obj.type,
            NADA: obj.nada,
        })

I thought of doing something such as

    output = []

    objs = Model.objects.filter(abc=abc) # this would return a some queryset
    for obj in objs:
        if obj.type == 'hello':
            output.append({
                TITLE: obj.title,
                TYPE: obj.type,
                WORD: obj.word,
            })
        if obj.type == 'world':
            output.append({
                TITLE: obj.title,
                TYPE: obj.type,
                NADA: obj.nada,
            })
    return output

the above should work but if there is another better way, I would love to know another way of doing this because the above seems too redundant.

Thanks in advance for any advices

2 Answers
Related