[Fixed]-How to join an django model object response

1👍

You should use values instead of values_list:

stuff = Source.objects.values('dbtype', 'host')

Then:

stuffs = [i['dbtype'] + '@' + i['host'] for i in stuff]
result = ', '.join(stuffs)

or:

def compose(stuff):
    return stuff['dbtype'] + '@' + stuff['host']

temp = map(compose, stuffs)
result = reduce(lambda x, y: x + ', ' + y, temp)

0👍

Firstly, values_list return list of tuples, so you can’t write Stuff.dbtype.

Secondly, str.join takes iterable object as parameter like list.

So you can rewrite your code like this:

Stuff = Source.object.values_list(dbtype, host)
result = ', '.join([dbtype + '@' + host for dbtype, host in Stuff])

Leave a comment