Saving to ProductImage

50 views
Skip to first unread message

Andrew Fam

unread,
Jun 4, 2015, 5:42:09 AM6/4/15
to mezzani...@googlegroups.com
I"m wondering how to create a new product image directly without the admin.

Using a package that defines the following

def upload_receive( request ):
"""
Returns the file(s) uploaded by the user.
"""
return request.FILES['files[]'] if request.FILES else None

I tried to create a Product image with the following which works in non cartridge fields,

f = upload_receive(request)
instance = ProductImage.objects.create(file=f, product_id=2)

I get *** TypeError: expected string or buffer



Ken Bolton

unread,
Jun 4, 2015, 9:54:08 AM6/4/15
to mezzanine-users
Hi Andrew,

Can you clarify your goals for us? I'm trying to think of a use-case where one would want ProductImages editable by non-admin users. What you want is certainly possible, but might end up causing problems with the overall experience of your users.

-ken

--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Fam

unread,
Jun 4, 2015, 11:06:30 AM6/4/15
to mezzani...@googlegroups.com
Hey Kenneth,

Client wanted a simpler form for adding in new products outside of the backend django admin because the receiving party are not really educated.
I'm using django jfu to take files and save them to the model as per the screenshot.

Could you explain how the system works?

Andrew Fam

unread,
Jun 4, 2015, 11:11:32 AM6/4/15
to mezzani...@googlegroups.com
One more clarification.

I'm building a multi tenanted Sass system for companies to go online. 
I have multiple owners coming into the system to add in their own products and all. 
The django admin has too much stuff going on so needed to simplify the forms used.


Andrew

Sam Kingston

unread,
Jun 4, 2015, 6:56:47 PM6/4/15
to mezzani...@googlegroups.com
Hi Andrew,

Try something like this (ripped from a shop I have written that syncs images from another database):


Hope this helps

Andrew Fam

unread,
Jun 4, 2015, 7:15:56 PM6/4/15
to mezzani...@googlegroups.com
Hey Sam,

Thanks so much. i'll give it a shot.

Best regards,
Andrew

Andrew Fam

unread,
Jun 4, 2015, 8:17:11 PM6/4/15
to mezzani...@googlegroups.com
Hey Sam you have missing imports.

Where do i get File(from)?
Also this uses a file which is on server.
How do i feed it a file which has been just uploaded.

Best regards,
Andrew




On Friday, June 5, 2015 at 6:56:47 AM UTC+8, Sam Kingston wrote:

Andrew Fam

unread,
Jun 4, 2015, 8:36:37 PM6/4/15
to mezzani...@googlegroups.com
Hey Sam,

the gist doesn't work. Still getting the same error

product.images.create(file=File(f), product_id = product.id)


On Friday, June 5, 2015 at 6:56:47 AM UTC+8, Sam Kingston wrote:

Sam Kingston

unread,
Jun 4, 2015, 8:38:38 PM6/4/15
to mezzani...@googlegroups.com

Can you paste in the full exception?

--
You received this message because you are subscribed to a topic in the Google Groups "Mezzanine Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mezzanine-users/tGaQWvZb4eg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mezzanine-use...@googlegroups.com.

Andrew Fam

unread,
Jun 4, 2015, 8:49:50 PM6/4/15
to mezzani...@googlegroups.com
upload_receive(request) gives <InMemoryUploadedFile: bg.png (image/png)>


f = File(upload_receive(request).open())
returns <File: None>
ProductImage.objects.create(file=f, product_id=2) just creates a ProductImage with blank file and description

 f = File(upload_receive(request))
returns <File: bg.png>
ProductImage.objects.create(file=f, product_id=2)
returns 
*** TypeError: expected string or buffer

Sam Kingston

unread,
Jun 4, 2015, 9:20:15 PM6/4/15
to mezzani...@googlegroups.com

Do you have a full trace back though? Without it, it is hard to see what the exception is referring to

Andrew Fam

unread,
Jun 4, 2015, 10:20:05 PM6/4/15
to mezzani...@googlegroups.com
  File "<string>", line 1, in <module>
  File "/Users/andrewfam/.virtualenvs/oshop/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/andrewfam/.virtualenvs/oshop/lib/python2.7/site-packages/django/db/models/query.py", line 346, in create
    obj = self.model(**kwargs)
  File "/Users/andrewfam/.virtualenvs/oshop/lib/python2.7/site-packages/django/db/models/base.py", line 470, in __init__
    setattr(self, field.attname, val)
  File "/Users/andrewfam/.virtualenvs/oshop/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 44, in __set__
    obj.__dict__[self.field.name] = self.field.to_python(value)
  File "/Users/andrewfam/.virtualenvs/oshop/lib/python2.7/site-packages/filebrowser_safe/fields.py", line 106, in to_python
    return FileObject(url_to_path(value))
  File "/Users/andrewfam/.virtualenvs/oshop/lib/python2.7/site-packages/filebrowser_safe/functions.py", line 59, in url_to_path
    value = mediaurl_re.sub('', value)
TypeError: expected string or buffer



Reply all
Reply to author
Forward
0 new messages