Hi,
> In my application I'm trying to serialize the following model to json:
>
> class Node(models.Model):
> name = models.CharField(db_column="NAME",
> max_length=30,
> db_index=True)
> description = models.CharField(db_column="DESCRIPTION",
> db_index=True,
> max_length=255)
> registrationDate=models.DateField(db_column="REGISTRATION_DATE",
> db_index=True)
> parentNodeObject = models.ForeignKey("self", db_column="PARENT",
> related_name="parent_fk")
>
> rootNodeObject= models.ForeignKey("self", db_column="ROOT",
> related_name="root_fk")
>
> Before I serialize the instances to json I retreived all the nodes:
> models.Node.objects.select_related
> ('parentNodeObject','rootNodeObject')
> The problem is that for all the parentNodeObjects and rootNodeObjects
> retrieved from DB, in the json string appears only the model's id.
> I don't know if this is normal or not but I'd like to have access to
> all the properties of all the objects retrieved,
> even if the final json string is huge.
This is normal behaviour -- the built-in model serializers traverse
only over local fields of the model and not over foreign keys.
> Is this possible?
Yes. In two ways:
1. If you are willing to write your own serializer, take a look at the
SERIALIZATION_MODULES setting and the interface you will need to
implement. To implement the interface, you would normally extend the
abstract base class: django.core.serializers.Serializer defined in
django/core/serializers/base.py
2. Create a list of dictionaries for your result set. Where each list
element represents one row of data and each row contains a dictionary
of the fields you are interested in. Then use
django.utils.simplejson.dumps to convert that list to a JSON string.
-RD
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---