Django, get a fully serialized object. All properties from many to many relationships

I am looking for a way to serialize a whole object with all its relationships. It seems I can get the primary key from the serializer.

This is my model:

class Action(models.Model):
    name = models.CharField('Action Name', max_length=250, unique=True)
    message = models.TextField('Message', blank=True, null=True)
    subject = models.CharField('Subject', max_length=40, null=True)
    recipient = models.ManyToManyField('Recipient', blank=True, null=True)

    def __str__(self):
            return unicode(self.name).encode('utf-8')

    def __unicode__(self):
            return unicode(self.name)

class Recipient(models.Model):
    address = models.EmailField('Address', max_length=75, unique=True)
    businessHours = models.BooleanField('Business Hours Only', default=False)

    def __str__(self):
            return unicode(self.address).encode('utf-8')

    def __unicode__(self):
            return unicode(self.address)

      

I am running the serializer here:

all = serializers.serialize('xml', list(Action.objects.select_related().all()))

      

if i start printing everything get output listing only the primary key for the recipient field.

<field type="CharField" name="subject">On The Road Productions</field>
<field to="sla.recipient" name="recipient" rel="ManyToManyRel">
<object pk="29"></object>
</field></object>
<object pk="11" model="sla.action">
<field type="CharField" name="name">On The Road Productions</field>
<field type="TextField" name="message">

      

I need all the properties for linked recipients. Any idea how I can get this?

edit - I found this snippet at

I found this snippet in xml_serializer.py, but I'm not sure how to modify it.

def handle_m2m_field(self, obj, field):
    """
    Called to handle a ManyToManyField. Related objects are only
    serialized as references to the object PK (i.e. the related *data*
    is not dumped, just the relation).
    """
    if field.creates_table:
        self._start_relational_field(field)
        for relobj in getattr(obj, field.name).iterator():
            self.xml.addQuickElement("object", attrs={"pk" :  smart_unicode(relobj._get_pk_val())})
        self.xml.endElement("field")

      

+2


a source to share


1 answer


offers a look at the insides of the django piston.



with django-piston it's not a problem for this and the code is pretty readable. so dive in.

0


a source







All Articles