Hi Bj�rn,
This didn't seem to get a response, but it deserves some acknowledgement... On Fri, 2006-12-29 at 04:17 +0000, Bj�rn Stabell wrote:
Hi, I would like to be able to serialize QuerySets for use as "canned" queries etc, and looking at QuerySet it's currently delegating __repr__ to its data. I was wondering what the feeling would be to change this to actually return a Python expression that would evaluate to the value of the QuerySet, for use in serializing etc. E.g., >>> a = MyModel.objects.filter(status=3).distinct()[:3] >>> a_str = repr(a) >>> a_str MyModel.objects.filter(status=3).distinct()[:3] >>> b = eval(a_str) >>> b MyModel.objects.filter(status=3).distinct()[:3] The object b would now be a clone of a. I haven't found a way to replicate it using filter() and exclude(), but can do it using complex_filter(), e.g., >>> b QuerySet(model=MyModel).complex_filter( QAnd( Q(), Q(status=3) )).distinct()
This seems like a reasonable idea, although I'm not sure if __repr__ is the right place for it or not (and that's something that doesn't interfere with the implementation anyway, so I'm not going to worry much). I would suggest/prefer holding off trying to get this perfect until the QuerySet rewrite is done (something I am doing). As part of that rewrite, QuerySets will contain a slightly more abstract version of the SQL being constructed -- the SQL string itself will be formed as part of the __str__ method. This removes a bunch of the hackery needed to join queries together and provides some manipulation possibilities we don't have now. The upshot of this for your work is that it will probably be easier to extract a serialised form of the query itself. I'm not sure how much easier/harder it will be to print it out as a Python statement like the above in all cases, but some form of serialisation that will be easy to dynamically reconstruct should be possible. That being said, I see no immediate problems with the patch (realising it's a prototype, so docs and tests are missing). I just wanted to possibly avoid a round of debugging for this if it's only going to have a lifespan of a month or two. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---
