On 28 Sep 2006, at 00:56, Steven Armstrong wrote:

>
> On 09/27/06 23:49, Tom Smith wrote:
>> Thanks, it all seems to work except for when I store a list of
>> objects in the database and try to get it out I get a string (that
>> looks like a list of objects)
>>
>>>>> products = Product.objects.filter(title__contains='Dog')[:10]
>>>>>  sub = Subscription(email="[EMAIL PROTECTED]", name="Tom Smith")
>>>>> sub.encoded_data = products
>>>>> sub.save()
>>
>
> You haven't encoded the data before assigning it to the "encoded_data"
> member.
>
>> ...... all fine.... then .....
>>
>>
>>>>> s = Subscription.objects.get(id=2)
>>>>> s.encoded_data
>> "[<Product: Go, Dog, Go (Beginner Books S.)>, <Product: The Practical
>> Dog Listener: The 30-Day Path to a Lifelong Understanding of Your
>> Dog>, <Product: Churchill's Black Dog and Other Phenomena of the
>> Human Mind>, <Product: The Dog Listener>, <Product: Dogsbody>,
>> <Product: Trixie Tempest and the Amazing Talking Dog: v. 1 (Trixie
>> Tempest, Tweenage Tearaway S.)>, <Product: A Dog's Best Friend: The
>> Secrets That Make Good Dog Owners Great>, <Product: The Seven Ages of
>> Your Dog: A Complete Guide to Understanding and Caring for Your Dog,
>> from Puppyhood to Old Age>, <Product: It's Me or the Dog: How to Have
>> the Perfect Pet>, <Product: Love That Dog>]"
>>
>>
>> .... returns it as listy-looking string, not a collection of
>> objects.... trying to get to data, returns this....
>>
>
> I guess django figures that the member "encoded_data" is a  
> TextField and
> converts your queryset to a string when assigning or saving it.
>
>>>>> s.data
>> Traceback (most recent call last):
>>    File "<console>", line 1, in ?
>>    File "/Users/tomsmith/Desktop/django/bah/../bah/burningahole/
>> models.py", line 160, in _get_data
>>      return self._decode(self.encoded_data)
>>    File "/Users/tomsmith/Desktop/django/bah/../bah/burningahole/
>> models.py", line 168, in _decode
>>      pickled = base64.decodestring(data)
>>    File "/Library/Frameworks/Python.framework/Versions/2.4//lib/
>> python2.4/base64.py", line 319, in decodestring
>>      return binascii.a2b_base64(s)
>> Error: Incorrect padding
>>>>>
>>
>
> Try this:
>
>>>> products = Product.objects.filter(title__contains='Dog')[:10]
>>>> sub = Subscription(email="[EMAIL PROTECTED]", name="Tom Smith")
>>>> sub.data = products
>>>> sub.save()
>
> You have to assign the products to the "data" member instead of to
> "encoded_data". Best to forget that the member encoded_data even  
> exists.
>
> So to store something:
> sub.data = myFancyPythonObject
>
> To retrieve something:
> myFancyPythonObject = sub.data
>
>
> Please note that I have never used this to store QuerySets or  
> Models or
> the like in the database. Just plain old python objects, preferably  
> dicts.
>
> By storing a QuerySet like this you decouple it from the database.
> I have no idea how this will behave when you decode and try to do
> something fancy with it later on. The database will most likely have
> changed in the mean time.

Thanks for the caveats... this is fantastic, this works a dream....

 >>> s = Subscription.objects.get(id=5)
 >>> for product in s.data:print product.title
...
Twins at St.Clare's
First Term at Malory Towers
Little Grey Rabbit's Birthday
Moldy Warp the Mole (Little Grey Rabbit Tape Library)
The Enchanted Horse
Enid Blyton on Tape: Malory Towers (Malory Towers S.)
Deadly Stranger (Nightmares on Tape S.)
Josie Smith
Mog and the Vee Ee Tee [Book and Cassette]
Good Work, Secret Seven

... but I think I'll take your advice and try to save these objects  
as dicts rather than objects

thanks again

tom













--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to