Re: Ajax request, json object, fields with instance of inherits class converted in regular string
you can also use simplejson and return data the way you want it. It isn't able to serialize date or time objects natively but this still covers 99% of the cases. from django.utils import simplejson from django.http import HttpResponse def view(request): return HttpResponse(simplejson.dumps([dict(monday=o.monday.name) for o in week.objects.all()]), mimetype="text/javascript") this method is a little more flexible and will be very useful if you have objects with more than one foreign key that you want to send back. On Feb 21, 11:28 am, Kev Dwyerwrote: > On Fri, 19 Feb 2010 07:00:36 -0800, manixor wrote: > > > > > > > The very big problem is, when I loop into object on template, in the > > monday field is not enymore the Day instance, but the string 1. How can > > I convert to an json object the week object, and to keep the instance of > > the inherits class, or how can I modify the ForeignField model, to point > > to another field, like name in my case and not to the pk, which is an > > integer in my case? > > Hello Maxinor, > > If I understand your problem correctly, I can think of two ways around this: > > (1) Use the development release (1.2, the natural keys > (http://docs.djangoproject.com/en/dev/topics/serialization/#natural-keys) > enhancement may do what you want. > (2) Set Day.name to have primary_key=True, that way the name will be > serialised instead of the automatically generated primary key. As > you have already set Day.name to be unique making it the primary key > shouldn't prove to be a problem. > > Cheers, > > Kev -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Re: Ajax request, json object, fields with instance of inherits class converted in regular string
On Fri, 19 Feb 2010 07:00:36 -0800, manixor wrote: > > The very big problem is, when I loop into object on template, in the > monday field is not enymore the Day instance, but the string 1. How can > I convert to an json object the week object, and to keep the instance of > the inherits class, or how can I modify the ForeignField model, to point > to another field, like name in my case and not to the pk, which is an > integer in my case? Hello Maxinor, If I understand your problem correctly, I can think of two ways around this: (1) Use the development release (1.2, the natural keys (http://docs.djangoproject.com/en/dev/topics/serialization/#natural-keys) enhancement may do what you want. (2) Set Day.name to have primary_key=True, that way the name will be serialised instead of the automatically generated primary key. As you have already set Day.name to be unique making it the primary key shouldn't prove to be a problem. Cheers, Kev -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Ajax request, json object, fields with instance of inherits class converted in regular string
Hello all, I have a really big problem with ForeignKey Models. I will try to emplain my problem: I have a class like this: class Day(models.Model): name= models.CharField(_('Name'), max_length=32, unique=True) description = models.TextField(_('Description'), max_length=1024, blank=True, null=True) and one more: class Week(models.Model): name= models.CharField(_('Name'), max_length=32, unique=True) description = models.TextField(_('Description'), max_length=1024, blank=True, null=True) monday = models.ForeignKey(Day, verbose_name=_('Monday'), blank=True, null=True, related_name='mondays') In my day table I have: name = "Always" description ="aaa" name = "Never" description ="bbb" In my week table I have: name = "Something" description ="fff" monday = 1 My problem is with the instance of the Day class in the monday field. 1 is the pk of the row with name "Always" In view I can do like this: ob = Week.object.all() for o in ob: o.monday.name and I will get "Always" instead of 1 (pk) OK, but, I need to send back to an ajax request this week object, with a json object, and I do: data = serializers.serialize("json", ob) The very big problem is, when I loop into object on template, in the monday field is not enymore the Day instance, but the string 1. How can I convert to an json object the week object, and to keep the instance of the inherits class, or how can I modify the ForeignField model, to point to another field, like name in my case and not to the pk, which is an integer in my case? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.