Discussion:
Deserialize json back to a queryset
Patrick Bassut
2015-04-21 02:23:39 UTC
Permalink
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?
--
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Filipe Ximenes
2015-04-22 14:17:29 UTC
Permalink
Sorry, what do you mean by "get back a queryset"?

On Mon, Apr 20, 2015 at 11:23 PM, Patrick Bassut <
Post by Patrick Bassut
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')])]
model = QuestionOption
exclude = ('is_correct',)
text = models.TextField()
is_correct = models.BooleanField(default=False)
objects = MasterQuerySetManager()
return self.filter(is_correct=True)
return self.filter(is_correct=False)
db_table = 'question_option'
return self.text[:50]
What method should I call to 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
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Patrick Bassut
2015-04-22 16:21:30 UTC
Permalink
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.

Em quarta-feira, 22 de abril de 2015 11:17:51 UTC-3, Filipe Ximenes
Post by Filipe Ximenes
Sorry, what do you mean by "get back a queryset"?
Post by Patrick Bassut
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')])]
model = QuestionOption
exclude = ('is_correct',)
text = models.TextField()
is_correct = models.BooleanField(default=False)
objects = MasterQuerySetManager()
return self.filter(is_correct=True)
return self.filter(is_correct=False)
db_table = 'question_option'
return self.text[:50]
What method should I call to 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
.
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Filipe Ximenes
2015-04-22 16:47:19 UTC
Permalink
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)


On Wed, Apr 22, 2015 at 1:21 PM, Patrick Bassut <
Post by Patrick Bassut
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.
Em quarta-feira, 22 de abril de 2015 11:17:51 UTC-3, Filipe Ximenes
Post by Filipe Ximenes
Sorry, what do you mean by "get back a queryset"?
Post by Patrick Bassut
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')])]
model = QuestionOption
exclude = ('is_correct',)
text = models.TextField()
is_correct = models.BooleanField(default=False)
objects = MasterQuerySetManager()
return self.filter(is_correct=True)
return self.filter(is_correct=False)
db_table = 'question_option'
return self.text[:50]
What method should I call to 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
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
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Patrick Bassut
2015-04-23 00:53:26 UTC
Permalink
That's what I want, indeed.
Any particular reason of why it's not possible? It seems like something we
would do very often.
--
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nemesis
2015-04-23 11:59:35 UTC
Permalink
Post by Patrick Bassut
That's what I want, indeed.
Any particular reason of why it's not possible? It seems like
something we would do very often.
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
--
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andy Baker
2015-04-23 17:32:34 UTC
Permalink
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.
Post by Nemesis
Post by Patrick Bassut
That's what I want, indeed.
Any particular reason of why it's not possible? It seems like
something we would do very often.
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
--
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...