Django Database Data Type

14 views
Skip to first unread message

Brendan Edwards

unread,
May 5, 2014, 7:39:19 AM5/5/14
to django...@googlegroups.com
Hi,

I have just started learning Django (been flip flopping between meteor and django, cant decide which to use..) and I am fairly new to web development in general.

Basically, I have a query where I am using the code "teecolor = GolfCourseTees.objects.values('Tee_Color').filter(Course_Name=course)" but I want to take the value retrieved form this and use it to make another query before loading the template. When I set color1= teecolor[0] color 1 will show as: {'Tee_Color': u'Blue'}, and I am unsure why this happens. I want to retrieve only the value "blue" in this case.

The reason for this is each course has usualy 4-5 tee colors. therefore I will have color1 = teecolor[0], color 2 = teecolor[1] etc.. so that I can insert that into a second query.

Questions:

1) What is the data output "{'Tee_Color': u'Blue'}" called and why does it display like this (I would like to understand and not just insert a given line of code)? I have read the documentation and I am still confused on this. 
2) How can I retrieve blue from {'Tee_Color': u'Blue'}? (example code would be helpful)

Brendan

Mike Dewhirst

unread,
May 5, 2014, 8:03:01 AM5/5/14
to django...@googlegroups.com
On 5/05/2014 5:39 PM, Brendan Edwards wrote:
> Hi,
>
> I have just started learning Django (been flip flopping between meteor
> and django, cant decide which to use..) and I am fairly new to web
> development in general.
>
> Basically, I have a query where I am using the code "teecolor =
> GolfCourseTees.objects.values('Tee_Color').filter(Course_Name=course)"

"objects" is the model manager. It returns a queryset which will
evaluate to none or more rows from the table. You make it evaluate by
iterating over it getting one row at a time or turning it into a list
like list(teecolor)

> but I want to take the value retrieved form this and use it to make
> another query before loading the template. When I set color1=
> teecolor[0] color 1 will show as: {'Tee_Color': u'Blue'}, and I am
> unsure why this happens. I want to retrieve only the value "blue" in
> this case.

The above queryset will return all the GolfCourseTees records for a
particular course.

>
> The reason for this is each course has usualy 4-5 tee colors. therefore
> I will have color1 = teecolor[0], color 2 = teecolor[1] etc.. so that I
> can insert that into a second query.

So getting a range of colors is the objective? Not sure how you want to
use that.

>
> Questions:
>
> 1) What is the data output "{'Tee_Color': u'Blue'}" called and why does
> it display like this (I would like to understand and not just insert a
> given line of code)? I have read the documentation and I am still
> confused on this.

This is a python dict with a key of 'Tee_Color' and a value being the
Unicode string 'Blue'

> 2) How can I retrieve blue from {'Tee_Color': u'Blue'}? (example code
> would be helpful)

>>> mydict = {'Tee_Color': u'Blue'}
>>> mydict['Tee_Color']
u'Blue'


hth

Mike

>
> Brendan
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a10e6d29-9fc9-483a-958f-f62385e7301b%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/a10e6d29-9fc9-483a-958f-f62385e7301b%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Brendan Edwards

unread,
May 5, 2014, 9:25:54 AM5/5/14
to django...@googlegroups.com
Thanks a lot for the reply! It got me thinking and I changed the code around a bit and was able to get the result I was looking for. What I did in the end was:

teecolor = GolfCourseTees.objects.filter(Course_Name='course').values()
tee1 = teecolor[0]

and in the HTML i used {{ tee1.hole1_distance }} etc.. which got me the correct value. I had tried this before but left out .values() due to lack of understanding at the time.

Thanks!

Brendan
Reply all
Reply to author
Forward
0 new messages