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)
)
You're printing the QueryList instead of the individual elements.
u', '.join(x.place for x in Q)
Override the __repr__
method if you want to change the way a Django model is printed in the shell.
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.
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
精彩评论