Hi,
Anyone can provide the further help? It seems this method has some
shortage, because the Continent is not related with Country. How can i
get the countries that under the certain continent by the ForeignKey:
continent=models.ForeignKey('Continent')?

On Mar 12, 6:50 pm, Ezequiel Marquez <exe.z...@gmail.com> wrote:
> El 12/03/11 13:31, yongzhen zhang escribió:
>
>
>
> > Hi, i want to rendering JSON from Django views, here is my models:
> > class Continent(models.Model):
> >     name = models.CharField(max_length='10',unique=True)
> >     code = models.CharField(max_length='5',unique=True)
> > class Country(models.Model):
> >     name = models.CharField(max_length='10',unique=True)
> >     code=models.CharField(max_length='10',unique=True)
> >     continent=models.ForeignKey('Continent')
> > ###############here is my views.py#######################
> > from django.http import Http404
> > from django.shortcuts import render_to_response, get_object_or_404
> > from django.utils import simplejson as json
> > from models import Continent, Country
>
> > def continent_json(request, continent_code):
> >     continent=get_object_or_404(Continent, code=continent_code)
> >     if not continent:
> >         raise Http404("Not implemented")
> >     context = {"all_contries":
> > Country.objects.filter(continent=continent.id)}
> >     data=json.dumps(context)
> >     return render_to_response(data, mimetype="application/json")
>
> > I want to get the result when the continent_code=eu:
> > {
> >   "xk": "Kosovo",
> >   "ch": "Switzerland",
> >   "gr": "Greece",
> >   "va": "Vatican City",
> >   "ee": "Estonia",
> >   "is": "Iceland",
> >   "al": "Albania",
> >   "gg": "Guernsey",
> > }
> > There is some problems with my views.py file. Anybody know how to
> > modify my views.py?
>
> try with this view
>
> def continent_json(request, continent_code):
>     continent=get_object_or_404(Continent, code=continent_code)
>     if not continent:
>         raise Http404("Not implemented")
>     context = [{'code': i.code, 'continent': i.name } for i in
> Country.objects.all() ]
>     data=simplejson.dumps(context)
>     return render_to_response(data, mimetype="application/json")
> --
> Ezequiel Marquez
>
>  signature.asc
> < 1KViewDownload

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to