Deserialize json back to a queryset

1,138 views
Skip to first unread message

Patrick Bassut

unread,
Apr 20, 2015, 10:23:40 PM4/20/15
to django-res...@googlegroups.com
Here's what I'm doing when trying to deserialize a json back to a queryset:
>>> from core.viewsets import *
>>> a = [{u'id': 278, u'text': u'552'},
...  {u'id': 287, u'text': u'in, on, in, At;'},
...  {u'id': 352, u'text': u'pH = 2,0'}]
>>> s = QuestionOptionSerializer(data=a, many=True)
>>> s.is_valid()
True
>>> s.data
[OrderedDict([('text', u'552')]), OrderedDict([('text', u'in, on, in, At;')]), OrderedDict([('text', u'pH = 2,0')])]


QuestionOptionSerializer is defined like this:

class QuestionOptionSerializer(serializers.ModelSerializer):
   
class Meta:
        model
= QuestionOption
        exclude
= ('is_correct',)


And the model, like this:

class QuestionOption(models.Model):
    text
= models.TextField()

    is_correct
= models.BooleanField(default=False)

    objects
= MasterQuerySetManager()

   
class QuerySet(MasterQuerySet):

       
def correct(self):
           
return self.filter(is_correct=True)

       
def incorrect(self):
           
return self.filter(is_correct=False)

   
class Meta():
        db_table
= 'question_option'

   
def __unicode__(self):
       
return self.text[:50]


What method should I call to get back a queryset?

Filipe Ximenes

unread,
Apr 22, 2015, 10:17:51 AM4/22/15
to django-res...@googlegroups.com
Sorry, what do you mean by "get back a queryset"?

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br

Patrick Bassut

unread,
Apr 22, 2015, 12:21:31 PM4/22/15
to django-res...@googlegroups.com
To get a queryset like when we do Model.objects.all().
I want the deserializer to return a queryset so I can add it to a ManyToMany relationship.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Filipe Ximenes

unread,
Apr 22, 2015, 12:47:43 PM4/22/15
to django-res...@googlegroups.com
Still not sure I got it.
From a data set, you want to get the queryset that generated it?
If so, it's not possible. The best you can do is to mount it from the given ids:
QuestionOption.objects.filter(id__in=list_of_ids)


To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Patrick Bassut

unread,
Apr 22, 2015, 8:53:26 PM4/22/15
to django-res...@googlegroups.com
That's what I want, indeed. 
Any particular reason of why it's not possible? It seems like something we would do very often.

Nemesis

unread,
Apr 23, 2015, 7:59:37 AM4/23/15
to django-res...@googlegroups.com
A queryset is an object representing the results of a query.

If you want a queryset you must query the database.

It is possible to implement it, but it sounds strange. Maybe if you
described more in detail your use case we might be able to give you some
more suggestions.

Federico

Andy Baker

unread,
Apr 23, 2015, 1:32:34 PM4/23/15
to django-res...@googlegroups.com
The key thing to remember is that a queryset isn't just a list of database rows. It's lazily evaluated for a start. But there's also the metadata involved - what were your filters and excludes? Did you have distinct()? How about prefetch_related and select_related and all that magic?

So you can probably get something a bit like a queryset for a given set of constraints but not something that you could swap for a queryset for all given usages.
Reply all
Reply to author
Forward
0 new messages