JSON serializers.serialize raises DoesNotExist on recursive data structures

129 views
Skip to first unread message

Legioneer

unread,
Mar 31, 2008, 5:39:05 AM3/31/08
to Django users
Hi All,

I'm trying to serialize recursive model (a category which refers to
itself) and get a 'DoesNotExist: Category matching query does not
exist', while other models work fine. Does anyone know a clue for
this?

I'm doing like this:

from django.core import serializers;
from newproject.models import Category

s = Category.objects.filter(parent__exact='0')
serializers.serialize("json", s[:5])

and get:
Traceback (most recent call last):
File "<console>", line 1, in ?
File "/usr/lib/python2.3/site-packages/django/core/serializers/
__init__.py", line 72, in serialize
s.serialize(queryset, **options)
File "/usr/lib/python2.3/site-packages/django/core/serializers/
base.py", line 48, in serialize
self.handle_fk_field(obj, field)
File "/usr/lib/python2.3/site-packages/django/core/serializers/
python.py", line 41, in handle_fk_field
related = getattr(obj, field.name)
File "/usr/lib/python2.3/site-packages/django/db/models/fields/
related.py", line 209, in __get__
rel_obj = self.field.rel.to._default_manager.get(**params)
File "/usr/lib/python2.3/site-packages/django/db/models/manager.py",
line 69, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/lib/python2.3/site-packages/django/db/models/query.py",
line 263, in get
raise self.model.DoesNotExist, "%s matching query does not exist."
% self.model._meta.object_name
DoesNotExist: Category matching query does not exist.



Anthony

Michael

unread,
Mar 31, 2008, 9:34:31 PM3/31/08
to django...@googlegroups.com
Your error is actually coming from the fact that you are not getting
any results from your filter query. The reason that the error is
appearing on the second line of your script is because filter is a
lazy call and isn't really made until you ask it to return objects in
this bit: s[:5]. Change your filter or call Category.objects.all() it
should work.

Hope that helps,
Michael

Legioneer

unread,
Apr 1, 2008, 10:26:28 AM4/1/08
to Django users
Thank you, Michael. Tried several combinations but it doesn't work
either.

1. First I tried to use Category.objects.all():
c = Category.objects.all()
serializers.serialize("json", c)

and got the same error.

2. Then tried to init some variable with s[:5] and the result didn't
change.

3. When I use some other model which doesn't refer to itself (is a non
recursive) it works fine even when using with lazy filter calls
similar to my first example.

Anthony

On Apr 1, 5:34 am, Michael <newmani...@gmail.com> wrote:
> Your error is actually coming from the fact that you are not getting
> any results from your filter query. The reason that the error is
> appearing on the second line of your script is because filter is a
> lazy call and isn't really made until you ask it to return objects in
> this bit: s[:5]. Change your filter or call Category.objects.all() it
> should work.
>
> Hope that helps,
> Michael
>

1234

unread,
Apr 1, 2008, 10:32:37 AM4/1/08
to django...@googlegroups.com
this is my example

class Type(models.Model):
    type = models.CharField('分类名称',maxlength=50,core=True)
    path = models.CharField('url地址',maxlength=250,blank=True,editable=False)
    typename = models.CharField('分类名称',maxlength=200,editable=False)
    parent = models.ForeignKey('self',related_name="munchie",null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)
  
    order_id=models.IntegerField ('排序',default=0)
    fid=models.IntegerField ('根ID',default=0,editable=False)
    cengshu=models.IntegerField ('层数',default=1,editable=False)



2008/4/1, Legioneer <legi...@gmail.com>:

Legioneer

unread,
Apr 1, 2008, 10:53:35 AM4/1/08
to Django users
My data model is very similar to your:

class Category(models.Model):
name = models.CharField(max_length=64)
descr = models.CharField(max_length=255, blank=True)
rank = models.PositiveIntegerField(default=10)
parent = models.ForeignKey('self', null=True, blank=True,
related_name='child_set')

but because of some reason it doesn't want to to go through JSON
serializer :(

Anthony

1234

unread,
Apr 1, 2008, 8:40:40 PM4/1/08
to django...@googlegroups.com
Is this model work on in admin?

2008/4/1, Legioneer <legi...@gmail.com>:

Legioneer

unread,
Apr 2, 2008, 5:07:58 AM4/2/08
to Django users
yes. in admin there are no problems with it.

On Apr 2, 4:40 am, 1234 <mydja...@gmail.com> wrote:
> Is this model work on in admin?
>
> 2008/4/1, Legioneer <legion...@gmail.com>:

Legioneer

unread,
Apr 2, 2008, 5:29:34 AM4/2/08
to Django users
Found a solution. It works when add fields parameter to serialize, but
only when without parent field:

e.g. serializers.serialize("json", c, fields='name,rank') works
perfect, while
serializers.serialize("json", c, fields='name,rank,parent') raises
DoesNotExist exception

where c = Category.objects.filter(parent__exact=0)
Reply all
Reply to author
Forward
0 new messages