tags:

views:

105

answers:

4

Excuse my total newbie question but how do I convert:

[<Location: London>] or [<Location: Edinburgh>, <Location: London>] etc

into:

'London' or 'Edinburgh, london'

Some background info to put it in context:

Models.py:

class Location(models.Model):
    place = models.CharField(max_length=100)

    def __unicode__(self):
        return self.place

class LocationForm(ModelForm):
    class Meta:
        model = Location

forms.py

class BookingForm(forms.Form):  
    place = forms.ModelMultipleChoiceField(queryset=Location.objects.all(), label='Venue/Location:', required=False)

views.py

def booking(request):
    if request.method == 'POST':
        form = BookingForm(request.POST) 
        if form.is_valid():
                place = form.cleaned_data['place'] 
                recipients.append(sender)
                message = '\nVenue or Location: ' + str(place)
                send_mail('Thank you for booking', message, sender, recipients)
)
+1  A: 

Override the __repr__ method if you want to change the way a Django model is printed in the shell.

Colorado
That won't change how the QueryList is displayed though.
Ignacio Vazquez-Abrams
The original question has been changed since I posted this answer. What's the recommended policy on that? Delete the answer? Leave it?
Colorado
+1  A: 

If it's a result from a query you're printing there, try

[x.name for x in result]

if name is the attribute containing the location's name.

jellybean
+7  A: 

You're printing the QueryList instead of the individual elements.

u', '.join(x.place for x in Q)
Ignacio Vazquez-Abrams
Great worked a treat (just used place instead of Q)
Rob B
A: 

You could do that with a regexp. Given your first example :

import re
s = "[<Location: London>]"
m = re.search("Location: (.*)>", s)
print m.group(1)
London
Guillaume Lebourgeois