Manually setting an ImageField gives a SQL error

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Manually setting an ImageField gives a SQL error

Corey-17

I'm writing an app that gathers images from other file sources and puts
them into a database.

To do this I have a model with an ImageField. When I set the field to
the filename, it doesn't quote it in the resulting SQL passed to the
backed.

Here is what I'm doing, exactly:

    doc.image = os.path.join(upload_to,'%s_%s_%s.%s' %
        (the_ad.ordernumber, doctype, doc.id, docext)
    )
    doc.save()

The sve() method gives an error tracing back to the UPDATE statement.
In the statement the 'image' field looks like
"image"=/path/to/the/file.ext, ...

What is the proper method to set this field?

Thanks,

Corey


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

Reply | Threaded
Open this post in threaded view
|

Re: Manually setting an ImageField gives a SQL error

Rajesh Dhawan

> The sve() method gives an error tracing back to the UPDATE statement.

Usually it helps get you better responses, if you include the actual
stack trace.

> In the statement the 'image' field looks like
> "image"=/path/to/the/file.ext, ...
>
> What is the proper method to set this field?

The ImageField's value is a string with the path of the image file
relative to your settings.MEDIA_ROOT. It is not the absolute path of
the image file.

You can easily check this by uploading an image through this field from
the Admin screen and then checking what the field contains for that
image.


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

Reply | Threaded
Open this post in threaded view
|

Re: Manually setting an ImageField gives a SQL error

Corey-17

Here is the stack trace:

------------------------------------------------------------------------
---
psycopg.ProgrammingError                          Traceback (most  
recent call last)

/home/django_dev/pennysaver/<ipython console>

/home/django_dev/pennysaver/addocs/processworkflows.py in  
start_check_workflows(debug)
     137             for i in wflow[1].split(".")[1:]:
     138                 mod = getattr(mod, i)
     139             function = getattr(mod, wflow[2])
--> 140             function(dds_file)
     141

/home/django_dev/pennysaver/addocs/workflows.py in handle_layout
(dds_xml_file)
     150         return
     151
--> 152     addoc, addocid = copy_to_db('Layout', dds_xml_file, ad, 2)
     153     docname,docext = os.path.splitext(addoc)
     154     copy_to_art(addoc, 'layouts', '%s.%s' % (addocid, docext))

/home/django_dev/pennysaver/addocs/workflows.py in copy_to_db
(workflow, dds_xml_file, the_ad, doct ype)
     120         (the_ad.ordernumber, doctype, doc.id, docext)
     121     )
--> 122     doc.save()
     123     return (dst_path, doc.id)
     124

/usr/lib/python2.4/site-packages/django/db/models/base.py in save(self)
     182                         ','.join(['%s=%%s' %  
backend.quote_name(f.column) for f in non_pks ]),
     183                         backend.quote_name
(self._meta.pk.column)),
--> 184                         db_values + [pk_val])
     185             else:
     186                 record_exists = False

/usr/lib/python2.4/site-packages/django/db/backends/util.py in execute
(self, sql, params)
      10         start = time()
      11         try:
---> 12             return self.cursor.execute(sql, params)
      13         finally:
      14             stop = time()

ProgrammingError: ERROR:  syntax error at or near "_2_8" at character  
118

UPDATE "addocs_document" SET  
"doctype"=2,"ad_id"='123456',"height"=0,"width"=0,"image"=ad_document  
s/2006/10/12/123456_2_8..pdf,"submitted"='2006-10-12  
12:43:24',"received"='2006-10-12 12:57:12.155 598' WHERE "id"=8


As you can see in the UPDATE statement, it is relative to the  
MEDIA_ROOT folder, and it is not quoted. (I don't know why it has two  
periods, but that's my bug)

Thanks for any help,

Corey


On Oct 12, 2006, at 3:12 PM, RajeshD wrote:

>
>> The sve() method gives an error tracing back to the UPDATE statement.
>
> Usually it helps get you better responses, if you include the actual
> stack trace.
>
>> In the statement the 'image' field looks like
>> "image"=/path/to/the/file.ext, ...
>>
>> What is the proper method to set this field?
>
> The ImageField's value is a string with the path of the image file
> relative to your settings.MEDIA_ROOT. It is not the absolute path of
> the image file.
>
> You can easily check this by uploading an image through this field  
> from
> the Admin screen and then checking what the field contains for that
> image.
>
>
> >


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

Reply | Threaded
Open this post in threaded view
|

Re: Manually setting an ImageField gives a SQL error

Rajesh Dhawan

>
> UPDATE "addocs_document" SET
> "doctype"=2,"ad_id"='123456',"height"=0,"width"=0,"image"=ad_document
> s/2006/10/12/123456_2_8..pdf,"submitted"='2006-10-12
> 12:43:24',"received"='2006-10-12 12:57:12.155 598' WHERE "id"=8
>
> As you can see in the UPDATE statement, it is relative to the
> MEDIA_ROOT folder, and it is not quoted.

Right...as you have surmised, the lack of quoting is indeed the cause
of the error.

Try this:

doc.image = str(os.path.join(upload_to,'%s_%s_%s%s' %
       (the_ad.ordernumber, doctype, doc.id, docext) ) )


(I don't know why it has two
> periods, but that's my bug)

If you are using os.path.splitext() to derive docext by any chance,
realize that the extension you get from that method already has the
dot in it i.e. '.pdf'. So, when you compose your filename from it you
just need '%s_%s_%s%s instead of '%s_%s_%s.%s'.


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

Reply | Threaded
Open this post in threaded view
|

Re: Manually setting an ImageField gives a SQL error

Rajesh Dhawan
In reply to this post by Corey-17

>
> UPDATE "addocs_document" SET
> "doctype"=2,"ad_id"='123456',"height"=0,"width"=0,"image"=ad_document
> s/2006/10/12/123456_2_8..pdf,"submitted"='2006-10-12
> 12:43:24',"received"='2006-10-12 12:57:12.155 598' WHERE "id"=8
>
> As you can see in the UPDATE statement, it is relative to the
> MEDIA_ROOT folder, and it is not quoted.

Right...as you have surmised, the lack of quoting is indeed the cause
of the error.

Try this:

doc.image = str(os.path.join(upload_to,'%s_%s_%s%s' %
       (the_ad.ordernumber, doctype, doc.id, docext) ) )


(I don't know why it has two
> periods, but that's my bug)

If you are using os.path.splitext() to derive docext by any chance,
realize that the extension you get from that method already has the
dot in it i.e. '.pdf'. So, when you compose your filename from it you
just need '%s_%s_%s%s instead of '%s_%s_%s.%s'.


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

Reply | Threaded
Open this post in threaded view
|

Re: Manually setting an ImageField gives a SQL error

Corey-17
In reply to this post by Rajesh Dhawan

Thanks for the tip, I am using os.path.splitext() !

I think somehow the problem lies with psycopg and it's "auto-quoting"  
feature.

I'm not sure how it figures out what needs to be quoted and what  
doesn't.

Thanks for the help, RajeshD!

Corey

On Oct 12, 2006, at 4:07 PM, RajeshD wrote:

>
>>
>> UPDATE "addocs_document" SET
>> "doctype"=2,"ad_id"='123456',"height"=0,"width"=0,"image"=ad_document
>> s/2006/10/12/123456_2_8..pdf,"submitted"='2006-10-12
>> 12:43:24',"received"='2006-10-12 12:57:12.155 598' WHERE "id"=8
>>
>> As you can see in the UPDATE statement, it is relative to the
>> MEDIA_ROOT folder, and it is not quoted.
>
> Right...as you have surmised, the lack of quoting is indeed the cause
> of the error.
>
> Try this:
>
> doc.image = str(os.path.join(upload_to,'%s_%s_%s%s' %
>        (the_ad.ordernumber, doctype, doc.id, docext) ) )
>
>
> (I don't know why it has two
>> periods, but that's my bug)
>
> If you are using os.path.splitext() to derive docext by any chance,
> realize that the extension you get from that method already has the
> dot in it i.e. '.pdf'. So, when you compose your filename from it you
> just need '%s_%s_%s%s instead of '%s_%s_%s.%s'.
>
>
> >


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