Re: Field.choices get all items as json

2018-05-20 Thread Peter of the Norse
Create a view that returns json.  At least if you want to use it in an ajax 
way.  Otherwise, I would recommend just passing it as an argument to the 
templates that use it.

import json

def years(request):
return HttpResponse(json.dumps(dict(Model.YEAR_IN_SCHOOL_CHOICES)), 
content_type=‘application/json')

The dict is there to make sure you get it as an object instead of an array of 
arrays.

> On May 18, 2018, at 1:20 AM, Majid Hojati  wrote:
> 
> In fact I want to convert all of these choises into json format and then send 
> them to user 
> thanks
> 
> On Thursday, May 17, 2018 at 8:46:42 PM UTC+4:30, C. Kirby wrote:
> if you just want to put that into json do:
> import json
> 
> YEAR_IN_SCHOOL_CHOICES = (
> ('FR', 'Freshman'),
> ('SO', 'Sophomore'),
> ('JR', 'Junior'),
> ('SR', 'Senior'),
> )
> 
> serialized_year_choices = json.dumps(Model.YEAR_IN_SCHOOL_CHOICES) 
> 
> 
> 
> 
> 
> On Thursday, May 17, 2018 at 10:52:15 AM UTC-4, Majid Hojati wrote:
> Hi,
> I have a Field.choices in my model. lets say it is:
> 
> YEAR_IN_SCHOOL_CHOICES = (
> ('FR', 'Freshman'),
> ('SO', 'Sophomore'),
> ('JR', 'Junior'),
> ('SR', 'Senior'),
> )
> 
> I can access them using this method Model. YEAR_IN_SCHOOL_CHOICES  but how 
> can I convert it to a json then I can send them using a webservice ? is there 
> any good method?
> 
> -- 
> 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+unsubscr...@googlegroups.com 
> .
> To post to this group, send email to django-users@googlegroups.com 
> .
> Visit this group at https://groups.google.com/group/django-users 
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/be3eabf9-939a-429f-a7c8-62ba30b98981%40googlegroups.com
>  
> .
> For more options, visit https://groups.google.com/d/optout 
> .

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1CF3C717-1324-48CF-B9D2-6A62A3EFF827%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Field.choices get all items as json

2018-05-18 Thread Majid Hojati
In fact I want to convert all of these choises into json format and then 
send them to user 
thanks

On Thursday, May 17, 2018 at 8:46:42 PM UTC+4:30, C. Kirby wrote:
>
> if you just want to put that into json do:
> import json
>
> YEAR_IN_SCHOOL_CHOICES = (
> ('FR', 'Freshman'),
> ('SO', 'Sophomore'),
> ('JR', 'Junior'),
> ('SR', 'Senior'),
> )
>
> serialized_year_choices = json.dumps(Model.YEAR_IN_SCHOOL_CHOICES) 
>
>
>
>
>
> On Thursday, May 17, 2018 at 10:52:15 AM UTC-4, Majid Hojati wrote:
>>
>> Hi,
>> I have a Field.choices in my model. lets say it is:
>>
>> YEAR_IN_SCHOOL_CHOICES = (
>>> ('FR', 'Freshman'),
>>> ('SO', 'Sophomore'),
>>> ('JR', 'Junior'),
>>> ('SR', 'Senior'),
>>> )
>>
>>
>> I can access them using this method Model. YEAR_IN_SCHOOL_CHOICES  but 
>> how can I convert it to a json then I can send them using a webservice ? is 
>> there any good method?
>>
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/be3eabf9-939a-429f-a7c8-62ba30b98981%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Field.choices get all items as json

2018-05-17 Thread C. Kirby
if you just want to put that into json do:
import json

YEAR_IN_SCHOOL_CHOICES = (
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
)

serialized_year_choices = json.dumps(Model.YEAR_IN_SCHOOL_CHOICES) 





On Thursday, May 17, 2018 at 10:52:15 AM UTC-4, Majid Hojati wrote:
>
> Hi,
> I have a Field.choices in my model. lets say it is:
>
> YEAR_IN_SCHOOL_CHOICES = (
>> ('FR', 'Freshman'),
>> ('SO', 'Sophomore'),
>> ('JR', 'Junior'),
>> ('SR', 'Senior'),
>> )
>
>
> I can access them using this method Model. YEAR_IN_SCHOOL_CHOICES  but 
> how can I convert it to a json then I can send them using a webservice ? is 
> there any good method?
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3ebbf6ad-9d34-44ec-8274-983cb5a5fa97%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Field.choices get all items as json

2018-05-17 Thread Majid Hojati
Hi,
I have a Field.choices in my model. lets say it is:

YEAR_IN_SCHOOL_CHOICES = (
> ('FR', 'Freshman'),
> ('SO', 'Sophomore'),
> ('JR', 'Junior'),
> ('SR', 'Senior'),
> )


I can access them using this method Model. YEAR_IN_SCHOOL_CHOICES  but how 
can I convert it to a json then I can send them using a webservice ? is 
there any good method?

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/42833f6b-0016-42ec-81c2-d37c9cd9a119%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.