Image Upload Issue with Symfony3, Doctrine 2 and Sonata Admin Bundle - doctrine-orm

i will upload multipe Images in an Sonta Admin Frontend with and Sonata_Form_Collection.
The upload is the following error in the log:
[2017-06-24 13:06:19] request.CRITICAL: Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: "Expected argument of type "AppBundle\Entity\Picture", "array" given" at /Users/Christian/Sites/zeltplatz/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 275 {"exception":"[object] (Symfony\\Component\\PropertyAccess\\Exception\\InvalidArgumentException(code: 0): Expected argument of type \"AppBundle\\Entity\\Picture\", \"array\" given at /Users/Christian/Sites/zeltplatz/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php:275)"} []
Here my Campground Entity:
https://pastebin.com/VWxRY9yq
and my Picture Entity:
https://pastebin.com/e0myy9b9
Also my Sonata Campground Admin:
https://pastebin.com/q3xnu6Ts
and Sonata Picture Admin:
https://pastebin.com/sJwaNUdi
The goal is that I can save several pictures for a campground.
Thanks for feedback and ideas

Here you have a problem with the data type, you need instance of Picture and you give array(of pictures i think). Maybe try add pictures one after the other in loop.

Related

Django funcion views, creating a company with user associated

So, i have this problem when im trying to create my "company", when i didnt have user authentication the view worked just fine. Now i've added django allauth, and this screen appears to me:
enter image description here
So i will leave here my model and my view to create the company
Im just a begginner so sry for my bad explanation
enter image description here
enter image description here
The URLS : https://prnt.sc/K-lKvmfuQtvR
It looks like the problem is with the way your url is setup. Do you have a screenshot of the urls.py as well so we can see how you setup your url mapping. (OP added this screenshot: prnt.sc/K-lKvmfuQtvR)
Your companies/<pk>/ endpoint is catching your "create_company" value and storing it as a variable. Try moving your companies/create_company path above your companies/<pk>/ like so
path('companies/', listing_companies),
path('companies/create_company/', company_create),
path('companies/<pk>', retrieve_companies),
path('companies/<pk>/update/', company_update),
path('companies/<pk>/delete/', company_delete),
Alternatively rename companies/create_company/ to create_company/ like so
path('companies/', listing_companies),
path('companies/<pk>', retrieve_companies),
path('create_company/', company_create),
path('companies/<pk>/update/', company_update),
path('companies/<pk>/delete/', company_delete),
The end goal in either case is simply to make sure your paths are not capturing your "create_company" and storing it as that variable that it is passing along to your path's method.

Why do I get an unexpected keyword argument?

I have created a model in my Djano project and the name of the model is Questions in which I have created a primary key called questionid. I am able to get all listings as summaries on one page however when I try to get a detailed listing of one query (say question number 4 out of the several in the table) by going to the http://127.0.0.1:8000/qanda/4 (here 4 is the question number), I get an error that says
TypeError at /qanda/4
question() got an unexpected keyword argument 'question_questionid'
Please see the code below
In my model's views file
def question(request):
questionsm = Questions.objects.order_by('-published_date').filter(is_published=True)
context = {
'questionid': questionsm
}
return render(request,'qanda/question.html', context)
In my model url file
path('<int:question_questionid>', views.question, name='question'),
I will appreciate any help. thanks
You are not passing the var to the view.
def question(request, **id_what_you_want**):
# and if you want an individual view, you should get by id
Questions.objects.get(pk=id_what_you_want)

django and url creation

I want to construct the following url
domain/edit/xray/<id>/<image_name>
where id is a model's id and image_name is the name of an image.
I have an app that will handle image editing with its own views lets say it mypil
mypil.views
def edit_xray(request, id, image_name):
....code to get image by name
....model by id and edit image using PIL
model = Model.objects.get(pk=id)
project/urls.py
url(r'^edit/', include(mypil.urls))
mypil.urls
url(r'^xray/(?P<id>)\d+/(?P<image_name>)\w+\.\w{3}$', mypil.views.edit_xray)
But when i try to view edit/xray/1/image.jpg (both id and image exist) i encounter this problem.
invalid literal for int() with base 10: ''
and the traceback shows me that line above
model=Model.objects.get(pk=id)
Does the empty string('') mean that it doesn't parse the id correctly from the URL? Isn't my url pattern correct?
EDIT: Damn my eyes....needed to put \d+ and the image reg pattern inside the parentheses
url(r'^xray/(?P<id>\d+)/(?P<image_name>\w+\.\w{3})$,...)
Sorry for posting a question...(How do i delete my own questions?)
The correct url is:
url(r'^xray/(?P<id>\d+)/(?P<image_name\w+\.\w{3})$', mypil.views.edit_xray)
You should put the pattern in the parentnesses.

Generating image url using carrierwave in rails

This is somewhat related my question about joins here. By default when I use listing.image.name in my search results view, it does a full query to find the image for every listing in my results array. It even does an extra query just to check if the listing has any images. So to avoid this, I'm adding the following to my Thinking Sphinx query:
#ts_params[:sql][:joins] = "INNER JOIN listing_images ON listing_images.listing_id = listings.id AND listing_images.position = 0"
#ts_params[:sql][:select] = "listings.*, listing_images.image as image_name, listing_images.id as image_id"
This works, however I'm not sure how to generate the full image_url using carrierwave. Previously, where it was doing an extra query per result, I was using listing.image.image_url(:sizename). So, I can find the image name and ID from my join as above, but how to I convert this to a full image url via carrierwave? Is there a built-in method to retrieve the url, that doesn't require an 'image' object?
I tried listing.image_id.image_url(:sizename) but it gave an undefined method error as expected.
From carrierwave's perspective, the answer is obvious:
user.avatar.url
user.avatar.thumbnail.url
Here, user is an instance of a model, and avatar is the field on the model with mount_uploader called on it. In your case this would be something like:
listing_image.image_name.url
listing_image.image_name.thumbnail.url
That probably doesn't work, though, because it looks like you may be loading your listing_image fields into the Listing instead of through includes (or the dreaded N+1 non-eager loads). You may need to resolve your other stackoverflow question before this one will be possible.
Edit:
Per our discussion on your other issue, once you've got a has_one :main_image for just the one ListingImage you want, you're going to use something like #listing.main_image.image_name.url and, if you have a version named "thumbnail", #listing.main_image.image_name.thumbnail.url.
I had the similar issue when I was fetching image using query and wanted to build image url using db field instead of issuing full sql query for each image.
I found out that with couple of field fetched from related image table we can build image which will not run any sql for image if we have three fields id, updated_at and image_name this field should be from the table where image is being saved. It could be from the main table where image is saved as separate column or completely separate table use to specially for image here is a sample code
It can be in your helper or in decorator as per your choice
def logo_url(id, updated_at, name)
return if id.blank? || updated_at.blank? || name.blank?
Company.new(id: id, updated_at: updated_at, logo: name).logo
end
and in view you can call this helper method as
<%= logo_url(company.id, company.updated_at, company.logo).url %>
This way you can have your image with url without executing sql query on each image.

Testing Image upload with Django and Webtest

Does anyone know how I can test the image upload with using WebTest. My current code is:
form['avatar'] =('avatar', os.path.join(settings.PROJECT_PATH, 'static', 'img', 'avatar.png'))
res = form.submit()
In the response I get the following error "Upload a valid image. The file you uploaded was either not an image or a corrupted image.".
Any help will be appreciated.
Power was right. Unfortunately(or not) I found his answer after I spend half an hour debugging the webtest. Here is a bit more information.
Trying to pass only the path to the files brings you the following exception:
webtest/app.py", line 1028, in _get_file_info
ValueError: upload_files need to be a list of tuples of (fieldname,
filename, filecontent) or (fieldname, filename); you gave: ...
The problem is that is doesn't told you that it automatically will append the field name to the tuple send and making 3 item tuple into 4 item one. The final solutions was:
avatar = ('avatar',
file(os.path.join(settings.PROJECT_PATH, '....', 'avatar.png')).read())
Too bad that there is not decent example but I hope this will help anyone else too )
Nowadays selected answer didn't help me.
But I found the way to provide expected files in the .submit() method args
form.submit(upload_files=[('avatar', '/../file_path.png')])
With Python 3:
form["avatar"] = ("avatar.png", open(os.path.join(settings.PROJECT_PATH, '....', 'avatar.png', "rb").read())