Is it ok to use double quotes instead of single quotes in Field.choices?

5 views
Skip to first unread message

Continuation

unread,
Dec 27, 2009, 3:12:30 AM12/27/09
to Django users
I have a model field:

my_field = models.IntegerField(choices=MY_CHOICES)

One choice in MY_CHOICES is:

(1, 'I'm looking for...'),

I got a syntax error from that. The word "for" in the string was
treated as a reserved word.

Now if I change the single quotes to double quotes, it seems to work:
(1, "I'm looking for..."),

But the documentation only shows the single quote usage. I wonder if
it's safe to use double quotes instead. If not, what can I do to avoid
the syntax error?


Xia Kai(夏恺)

unread,
Dec 27, 2009, 3:18:01 AM12/27/09
to django...@googlegroups.com
Hi,

Please note that you have close the quotation with a single quote right
after the character `I`, so the next few word are treated as python source
code instead of string.

Probably you would like to try:

(1, "I'm looking for..."),

or, alternatively:


(1, 'I\'m looking for...'),


------------------------
Xia Kai(夏恺)
xia...@gmail.com
http://blog.xiaket.org

--------------------------------------------------
From: "Continuation" <selfor...@gmail.com>
Sent: Sunday, December 27, 2009 4:12 PM
To: "Django users" <django...@googlegroups.com>
Subject: Is it ok to use double quotes instead of single quotes in
Field.choices?

Continuation

unread,
Dec 27, 2009, 3:42:03 AM12/27/09
to Django users
Thank you. I totally missed that.

On Dec 27, 3:18 am, Xia Kai(夏恺) <xia...@gmail.com> wrote:
> Hi,
>
> Please note that you have close the quotation with a single quote right
> after the character `I`, so the next few word are treated as python source
> code instead of string.
>
> Probably you would like to try:
>
> (1, "I'm looking for..."),
> or, alternatively:
> (1, 'I\'m looking for...'),
>
> ------------------------
> Xia Kai(夏恺)
> xia...@gmail.comhttp://blog.xiaket.org
>
> --------------------------------------------------

> From: "Continuation" <selforgani...@gmail.com>

Chris Withers

unread,
Dec 27, 2009, 4:26:56 AM12/27/09
to django...@googlegroups.com
Continuation wrote:
> Now if I change the single quotes to double quotes, it seems to work:
> (1, "I'm looking for..."),

Double quotes are absolutely fine and a lot nicer to look at than
'I\'m hard to read'.

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Masklinn

unread,
Dec 27, 2009, 7:22:02 AM12/27/09
to django...@googlegroups.com
On 27 déc. 2009, at 09:12, Continuation <selfor...@gmail.com>
wrote:

This is a python issue, and in python single and double quotes are
basically equivalent (the only constraint is that you close a string
with the quote with which you opened it). As others explained the
issue here is that your apostrophe is a single quote, closing the
string and interpreting the rest if the line as python source… which
is obviouly incorrect.


Biju Varghese

unread,
Dec 27, 2009, 1:20:26 PM12/27/09
to Django users
Dear ,
Just check how you are creating that string.(1,'i'm looking for...')
you are giving it like 'i'm .
Effectively your string will be only 'I' and remaining part will be
taken as a undefined variable or something like that. if you want to
make it correct you have to add escape sequence like 'i\'m looking
for ..'
this will work.basically you have to add an escape character if you
want to add ' or " inside a string..

Reply all
Reply to author
Forward
0 new messages