Hi Tobia,

I have just discovered your post and I am trying to do something very
similar (create and XML doc from an object hierarchy).
I have started by trying to use the @toxml decorator and override the
__serialize__ method on my model. See (partially working) code below.

@toxml
class Risk(models.Model):
    name = models.CharField(max_length=50, blank=True)
    user = models.ForeignKey(User, related_name='risk')

    def __unicode__(self):
        return self.name

    def __serialize__(self, stream):
        stream.startElement(self._meta.object_name, {})
        for field in self._meta.fields:
            stream.startElement(field.name, {})
            stream.characters(field.value_to_string(self))
            stream.endElement(field.name)
        for relobj in self._meta.get_all_related_objects():
            o = relobj.name.partition(':')
            m = models.get_model(o[0], o[2])
            stream.startElement(m._meta.object_name, {})
            for f in m._meta.fields:
                stream.startElement(f.name, {})
                stream.characters(f.value_to_string(m))
                stream.endElement(f.name)
            stream.endElement(o[2])
        stream.endElement(self._meta.object_name)

My problem is 'stream.characters(f.value_to_string(m))' raises an
exception when the field is a Foreign Key.

However reading your question I am wondering if I am using the right
approach at all. Could you let me know if you think your method is
better and maybe provide a few code snippets to point me in the right
direction (if you think I am on the wrong track).

Thanks, Guy.


On Sep 15, 5:43 pm, Tobia Conforto <tobia.confo...@gruppo4.eu> wrote:
> Hi all
>
> I'm adding an XML mapping feature to Django db models.
>
> The idea is to add as little XML mapping information as possible to existing
> models (such as: which fields get mapped to XML, what is their XPath...) in
> order to be able to automatically:
> 1. produce an XML representation of an object;
> 2. parse a valid XML representation into a new object; and
> 3. get an XSD schema of the model.
>
> I'm well ahead. I have all three features working for some simple models.
> But I've hit a wall with regard to object relations, or ForeignKey fields.
>
> Apparently any subclasses of ForeignKey are completely ignored by Django at
> runtime, even if they declare __metaclass__ = SubfieldBase
>
> I guess they would need a special kind of metaclass, which I guess has not
> been written yet.
>
> Can anybody help me understand what piece is failing?
> How can I make it work?
>
> -Tobia
>
> Here is a (non) working example:
>
> ### test/models.py:
>
> from django.db.models import *
>
> # subclassing works for any simple field type, why not related fields?
> class MyForeignKey(ForeignKey):
> __metaclass__ = SubfieldBase
>
> def __init__(self, *args, **kwargs):
> ForeignKey.__init__(self, *args, **kwargs)
>
> class TestSub(Model):
> pass
>
> class Test(Model):
> sub = MyForeignKey(TestSub)
>
> ### shell:
>
> >>> from test.models import Test, TestSub
> >>> ts = TestSub.objects.create()
> >>> t = Test.objects.create(sub=ts)
>
> Traceback (most recent call last):
>   ...
> AttributeError: 'Test' object has no attribute 'sub_id'
>
>
>
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to