heywbj / django-rest-framework-recursive

Recursive Serialization for Django REST framework
ISC License
367 stars 39 forks source link

Run raw queries #7

Closed ctippur closed 9 years ago

ctippur commented 9 years ago

Hello,

I need to be able to run query on a recursive model that gives the name of the parent and child

My model

class RecursiveModel(models.Model): stage_title = models.CharField(max_length=255, unique=True) parent = models.ForeignKey('self', null=True)

I need to get the names of parent and child relationship Equivalant query being:

select c.id, c.stage_title as child, p.stage_title as parent from recursivemodel c, recursivemodel p where c.parent_id=p.id

How do I do that?

ctippur commented 9 years ago

When I run it, I get:

Cannot assign "'stgabc'": "RecursiveModel_Deferred_created_data_parent_id_stage_title.parent" must be a "RecursiveModel" instance.

heywbj commented 9 years ago

This question has nothing to do with recursive serialization.

instance  = RecursiveModel.objects.select_related('parent').get(id=...)
print(instance.name)
print(instance.parent.name)