M2M field with intermediary model doesn't play well with serializers in 2.0

Showing 1-3 of 3 messages
M2M field with intermediary model doesn't play well with serializers in 2.0 Kit La Touche 10/26/12 1:48 PM
rest_framework/serializers.py:440 fails if you have a M2M field on a model with an intermediary model defined.

It throws a predictable AttributeError("Cannot set values on a ManyToManyField which specifies an intermediary model.  Use %s.%s's Manager instead." % (opts.app_label, opts.object_name))

Any suggestions for a work-around? Or a bugfix? My instinct says to check for .through._meta.auto_created, but what the most natural and correct behavior to use if it's false is, I don't know.

Cheers,

--Kit
Re: M2M field with intermediary model doesn't play well with serializers in 2.0 Tom Christie 10/26/12 2:29 PM
Forgetting about the implementation details for a moment...

What behavior would you want to see from m2m with intermediary data?
Can you draft up some examples of how representations should be serialized and deserialized, and how to deal with create and update cases?  Can you draft up a failing test case with some expected behavior that isn't current observed?

I think the answer might be that if you need writable m2m-through data, then you need to re-consider the representation you're using.

Taking the example in the docs:


Your Group and Person serializers could include a read-only related field across the 'members' relationship, but they couldn't include a writable related field across the relationship (because there'd be no way of specifying the required extra intermediate data)

Instead you'd want an explicit representation of the Membership (ie a Membership serializer and associated views), and have the Group and Person serializer include a writable related field to the membership_set. (rather than directly to each other)

That feels right to me, and I think is similar behavior to the way that eg. Django's fixture serializers would represent the data(?)

Would def appreciate feedback from you on that.

Also it'd be a really good thing to get documented - dealing with relationships and representations of relationships, especially for writable relationships is tricky stuff.
Re: M2M field with intermediary model doesn't play well with serializers in 2.0 Kit La Touche 10/26/12 6:38 PM
"I think the answer might be that if you need writable m2m-through data, then you need to re-consider the representation you're using."

Yep, that. As I dug more into things, I increasingly saw that this was the case.

What you describe as "feels right" is. Thanks for the sanity check!

--Kit