From 8e79e35efe923817201775347a7c5de9498674b1 Mon Sep 17 00:00:00 2001 From: Greg Turner Date: Wed, 3 Nov 2010 15:25:18 +1100 Subject: [PATCH] mongo get_or_create doesn't like lists in queries. Workaround. --- xmltools/lib/xml2dict.py | 4 ++-- xmltools/processors/mongo.py | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/xmltools/lib/xml2dict.py b/xmltools/lib/xml2dict.py index e999d40..1b5d232 100644 --- a/xmltools/lib/xml2dict.py +++ b/xmltools/lib/xml2dict.py @@ -4,10 +4,10 @@ def xml2dict(tag): r = {} #value + # tag.text is None, for empty tags. if tag.text is not None: v = tag.text.strip() - if v: - r['_value'] = v + r['_value'] = v #attributes if tag.keys(): diff --git a/xmltools/processors/mongo.py b/xmltools/processors/mongo.py index eb91964..7b94628 100644 --- a/xmltools/processors/mongo.py +++ b/xmltools/processors/mongo.py @@ -30,4 +30,12 @@ def __call__(self, tag): d = xml2dict(tag) d = self.clean(d) - m, created = self.model.objects.get_or_create(**d) \ No newline at end of file + if d is not None: + try: + m = self.model.objects.get(id=d['id']) + for k, v in d.iteritems(): + setattr(m, k, v) + m.save() + except self.model.DoesNotExist: + m = self.model.objects.create(**d) + m.save() \ No newline at end of file