Django

Code

Ticket #11310 (closed: worksforme)

Opened 8 months ago

Last modified 7 months ago

Unable to serialize ManyToMany fields with "trough"

Reported by: Pavel Schön <pavel@schon.cz> Assigned to: nobody
Milestone: Component: Serialization
Version: SVN Keywords: ManyToMany
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Problem is, that ManyToMany? field with intermediate table is ignored in PythonSerializer?. This patch should fix it.

http://code.djangoproject.com/svn/django/trunk/django/core/serializers/python.py
59,61c59,60
<         if field.creates_table:
<             self._current[field.name] = [smart_unicode(related._get_pk_val(), strings_only=True)
<                                for related in getattr(obj, field.name).iterator()]
---
>         self._current[field.name] = [smart_unicode(related._get_pk_val(), strings_only=True)
>             for related in getattr(obj, field.name).iterator()]

Attachments

Change History

06/12/09 03:57:48 changed by Pavel Schön <pavel@schon.cz>

  • needs_better_patch changed.
  • component changed from Uncategorized to Serialization.
  • needs_tests changed.
  • keywords set to ManyToMany.
  • needs_docs changed.
  • has_patch set to 1.

07/11/09 09:08:52 changed by russellm

  • status changed from new to closed.
  • resolution set to worksforme.

The test suite contains a test to validate that serialization works for m2m with a through field (see the [http://code.djangoproject.com/browser/django/trunk/tests/regressiontests/m2m_through_regress/models.py regressiontests/m2m_through_regress tests). Without a test case showing a specific failure case I'm going to mark this worksforme.

07/29/09 04:09:28 changed by pavel@schon.cz

  • status changed from closed to reopened.
  • version changed from 1.0 to SVN.
  • resolution deleted.

Hi, look at this example:

class A(models.Model):
        foo = models.CharField(max_length=32)
#       b = models.ManyToManyField('B', through='A_B')
        b = models.ManyToManyField('B')

class B(models.Model):
        bar = models.CharField(max_length=32)

class A_B(models.Model):
        a = models.ForeignKey('A')
        b = models.ForeignKey('B')

Then i run:

>>> from django.core.serializers import serialize
>>> serialize('python', A.objects.all())
[{'pk': 1L, 'model': u'testapp.a', 'fields': {'foo': u'eeeee', 'b': [1L]}}]

This is OK.

Now I change models to this:

class A(models.Model):
        foo = models.CharField(max_length=32)
        b = models.ManyToManyField('B', through='A_B')
#       b = models.ManyToManyField('B')

class B(models.Model):
        bar = models.CharField(max_length=32)

class A_B(models.Model):
        a = models.ForeignKey('A')
        b = models.ForeignKey('B')

I serialize it:

>>> from django.core.serializers import serialize
>>> serialize('python', A.objects.all())
[{'pk': 1L, 'model': u'testapp.a', 'fields': {'foo': u'eeeee'}}]

'b' is missing in output...

07/29/09 04:54:48 changed by russellm

  • status changed from reopened to closed.
  • resolution set to worksforme.

Your example code is explicitly serializing objects of type A. However, when you have a through relationship, you need to serialize the A_B model separately, since that model could contain extra data.

When you don't have a through model, there isn't an actual model representing the through relationship., so the m2m data is included on the A model as a list of related objects.

07/29/09 05:15:21 changed by pavel@schon.cz

Thanks for explanation. I was migrating some models without 'through' to use 'through' and serialization behaviour was changed. I made a wraparound of this using custom serializer:

class CustomSerializer(python.Serializer):
	def handle_m2m_field(self, obj, field):
		field.creates_table = True
		super(XMLRPCSerializer, self).handle_m2m_field(obj, field)

Yeah, it's ugly, but it works ;-)

07/29/09 05:17:04 changed by pavel@schon.cz

s/XMLRPCSerializer/CustomSerializer/g


Add/Change #11310 (Unable to serialize ManyToMany fields with "trough")




Change Properties
Action