This looks to have been an issue with cStringIO. If I import StringIO instead it seems to work, but only after I create the object. I'd really like to do this:
test = PdfFile(business=business1, pdf = f, created = datetime.now()) test.save() however I get this traceback:http://dpaste.com/hold/97272/ I tried to add a name to f with f.name = 'test.pdf' and I get: In [21]: f.name = 'test.pdf' --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) AttributeError: can't set attribute If I were able to set a name I would be able to create the PdfFile object with the pdf included (which was my ultimate goal) However with the current solution I can create the object without the pdf then add it directly after... On Mon, Dec 8, 2008 at 11:06 PM, Timboy <[EMAIL PROTECTED]> wrote: > I'm having an issue passing a buffered StringIO to the FileField > object. > > Here is my code: > > ##models.py > from django.db import models > from business.models import Business > > class PdfFile(models.Model): > business = models.ForeignKey(Business) > pdf = models.FileField(upload_to='get_pdf_path') > created = models.DateTimeField(editable=False) > > ##console > from cStringIO import StringIO > from reportlab.pdfgen import canvas > from pdf.models import PdfFile > from business.models import Business > from django.core.files.base import ContentFile > from datetime import datetime > > buffer = StringIO() > > p = canvas.Canvas(buffer) > > p.drawString(100, 100, "Hello world.") > > p.showPage() > p.save() > business1 = business.objects.get(id=1) > > f = ContentFile(buffer.getvalue()) > PdfFile.pdf.save('blah.pdf', f) > ########################### > I've tried this as well: > path = default_storage.save('pdf/1/1.pdf', ContentFile(buffer.getvalue > ())) > test = PdfFile(listing = listing1,pdf=path, created = datetime.now()) > test.save() > > The issue with thtis is that the path object actually creates the file > and then when I pass it to save it actually sets the file to my media > directory/test and doesn't name the pdf or even show the proper pdf > dir. > > My goal is to pass my StringIO object directly to the FileField for > upload to my upload_to dir... > > TIA > --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---