class Bar(models.Model):
user = models.ForeignKey(User)
name = models.CharField(maxlength=50)
description = models.TextField()
where user is tied to a django.contrib.auth.models.User entity. When I
serialize this to json, I get:
[{"pk": "1", "model": "foo.bar", "fields": {"description": "", "user":
1, "name": "Phone"}}]
Which is fine, but I need both the username and user id on the front
end. Is there a simple way to get this info all in one shot that I'm
just missing?
Ideally, it would return something like: ..."user":
{"username":"bfordham", "pk":1}...
I'm using the django-rest-interface, if that makes a difference one way
or the other.
Thanks
--B
The serializer doesn't support this. It would take a bit of design work
to figure out how to specify such an extension easily. You might like
put some thought into that, though.
Things that immediately spring to mind as requiring addressing:
- what should the format look like for many-to-many objects (and
making sure we can deserialise as well)
- once you add one level of indirection, it will take
approximately 8 seconds for somebody to want to go two levels
and more. So it should be extensible beyond just "the immediate
foreign key relative".
- reverse relations
- deserialisation, if possible.
- is it easy to say "all the normal fields plus these extra
ones".
- could passing a ValuesQuerySet to the serializer, together
with #5768 be a solution in some fashion (as a way of specifying
these extra fields)?
I don't know how easy this would all be or how genuinely useful. Could
be worth a bit of thinking, though.
Malcolm
--
I just got lost in thought. It was unfamiliar territory.
http://www.pointy-stick.com/blog/
For my specific case, I wrote a custom serializer, extending the current
json one, that added the additional info I want from User. It won't
deserialize properly, but I don't care for this application.
I put a bit of thought into it this afternoon, and I may play around with
seeing if any of them would actually work. What I have is good enough for
me in this particular case, but again it's little more than a hack, and
certainly not something that should be merged in anywhere 8)
Thanks
--B
I'll play with this and see how it works out tomorrow
Thanks
--B
With a bit of prompting from another Djangoista I've posted the python
and json "full" serializer implementations to http://code.djangoproject.com/ticket/4656
.
Please have a try and let me know if you find and bugs or have any
other feedback.
cheers
Matthew
On Dec 14 2007, 5:02 pm, "mattimust...@gmail.com"
> 'email': u'john....@example.com',
> [1]http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_reco...
>
> regards
>
> matthew
> --
> Matthew Flanaganhttp://wadofstuff.blogspot.com