Serializing objects with a many-to-many reference

2,118 views
Skip to first unread message

Jim N

unread,
Mar 2, 2010, 11:46:17 AM3/2/10
to Django users
Hi,

I am writing a question-and-answer app which serializes data in JSON.
I have Question, User, and Asking models. Asking is the many-to-many
relationship table for Question and User, because the Asking
relationship may be more complicated than it seems. (Several users
may ask, and re-ask the same question, and I need to store when the
question was asked, whether the asker is primary or secondary, publish
date for the question, etc.)

The problem comes when I serialize a Question. I want to get the User
information in the serialized object.

Models:

class User(models.Model):
alternate_id = models.CharField(max_length=200, null=True)
identifier = models.CharField(max_length=200, null=True)

class Asking(models.Model):
user = models.ForeignKey(User)
question = models.ForeignKey(Question)
is_primary_asker = models.BooleanField()

class Question(models.Model):
text = models.TextField()
user = models.ManyToManyField('User', through='Asking', null=True)

In the view:

questions = Question.objects.filter(**filters)
return serializers.serialize("json", questions)

That's not giving me anything but a user Id.

I looked at natural keys, but I'm using Django 1.1.1.

Thanks for any suggestions.

-Jim

Russell Keith-Magee

unread,
Mar 2, 2010, 6:45:23 PM3/2/10
to django...@googlegroups.com

The short answer is you can't - at least, not out of the box. This is
a feature that has been proposed several times [1] in the past.
Django's serializers are primarily designed for use in the testing
system, where the exact structure of the serialized output isn't as
important.

At some point, I'm hoping to be able to do a full teardown of the
serialization infrastructure to make it completely configurable. This
would enable features like [1], along with many others.

In the interim, there is a third-party project called "Django Full
Serializers" [2] that includes this sort of functionality.

[1] http://code.djangoproject.com/ticket/4656
[2] http://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers

Yours,
Russ Magee %-)

Jim N

unread,
Mar 3, 2010, 10:21:12 AM3/3/10
to Django users
Thanks Russell,

I ended up writing it myself - breaking the QuerySet into a
dictionary, grabbing and plugging in the information from the other
model, and then re-serializing it not using the serializer.serialize,
but the simplejson.dumps.

I'll check out DjangoFullSerializers for future reference though. It
seems like this kind of thing must come up all the time.

-Jim

On Mar 2, 6:45 pm, Russell Keith-Magee <freakboy3...@gmail.com> wrote:

Jim N

unread,
Mar 3, 2010, 12:36:23 PM3/3/10
to Django users
I just came across manager methods in the docs:

http://docs.djangoproject.com/en/dev/topics/db/managers/#adding-extra-manager-methods

Could I have used these to create a seriallizable QuerySet, by
defining, say, a with_user() method inside the Questions model?

-Jim

Russell Keith-Magee

unread,
Mar 3, 2010, 6:23:05 PM3/3/10
to django...@googlegroups.com
On Thu, Mar 4, 2010 at 1:36 AM, Jim N <jim.n...@gmail.com> wrote:
> I just came across manager methods in the docs:
>
> http://docs.djangoproject.com/en/dev/topics/db/managers/#adding-extra-manager-methods
>
> Could I have used these to create a seriallizable QuerySet, by
> defining, say, a with_user() method inside the Questions model?

If you take this approach, you'll hit up against #5711. Django doesn't
allow for serialization of arbitrary model attributes. This is also
something that needs to be addressed as part of the serialization
teardown.

Yours,
Russ Magee %-)

Jesus Mager

unread,
Mar 9, 2010, 12:57:49 PM3/9/10
to django...@googlegroups.com
2010/3/2 Russell Keith-Magee <freakb...@gmail.com>:

> On Wed, Mar 3, 2010 at 12:46 AM, Jim N <jim.n...@gmail.com> wrote:
>> Hi,

> The short answer is you can't - at least, not out of the box. This is


> a feature that has been proposed several times [1] in the past.
> Django's serializers are primarily designed for use in the testing
> system, where the exact structure of the serialized output isn't as
> important.
>
> At some point, I'm hoping to be able to do a full teardown of the
> serialization infrastructure to make it completely configurable. This
> would enable features like [1], along with many others.
>
> In the interim, there is a third-party project called "Django Full
> Serializers" [2] that includes this sort of functionality.
>
> [1] http://code.djangoproject.com/ticket/4656
> [2] http://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers
>
> Yours,
> Russ Magee %-)
>


Hi russell!

I'm trying to gather all things that are wanted and requested for
customizable serialization. I see custom in-depth serialization is
requested. But, what other features are wanted?

--
Jesus Mager
[www.h1n1-al.blogspot.com]

Russell Keith-Magee

unread,
Mar 9, 2010, 6:58:55 PM3/9/10
to django...@googlegroups.com

The ideal end goal is that you should be able to output any object (or
list of objects), in any format, to any depth, with any additional
information. This includes, but is not limited to:

* Serializing nested structures (of arbitrary depth)
* Serializing subsets of model attributes
* Serializing non-database attributes/properties
* Serialized output that doesn't match the current default output
format (i.e., a model in JSON doesn't have to be {"pk": XX, "model":
"myapp.foo", "fields": {...}} )
* Serialized output format that can change on a per-model basis
* Serialized output format that can change based on where in the
output tree the object is located (e.g., output the full User object
if it's included from within model X, but only output the username if
its included from within model Y)

In short, we want serialization to be flexible. If we've made a design
decision with Django's serializers, that decision should be
customizable as an end user.

Yours
Russ Magee %-)

Reply all
Reply to author
Forward
0 new messages