Invalid literal for int() with base 10: 'csv' - django

I'm trying to export the user details from the auth_user table to a CSV file in the Django admin panel. I have written the export to CSV function correctly and it has worked fine with other tables of mine. I have also provided the function location in urls.py correctly as:
(r'^auth/user//csv/', 'catalyst_db.catalyst.utils.admin_user_export'),
But Django has provided me the error saying that:
ValueError at /auth/user/csv/ invalid literal for int() with base 10: 'csv'
Any ideas to overcome this problem?

You probably have a previous URL in your urlconf which is matching the CSV value before it gets to that pattern.

I believe you have a date(User model has a join date field) inside the csv file that you are trying to upload.The date field may be corrupt,most probably contains an aphotrophe before the date entry.(You can see the aphostrophe in the formula bar).
This can be fixed by the find and replace of your spreadsheet application.
Replace ^. with & in regular expression mode.

Related

Is it possible to query Django objects by their FileField's url attribute

I have a Django class with a FileField. I know that you can get the FileField's url, or path, by calling filefield.path, or filefield.url . I tried to query for all of those objects by their FileField's url using
media = MediaLibraryFile.objects.filter(media_file__url='some_key')
but I get this error.
Unsupported lookup 'url' for FileField or join on the field not permitted.
I looked around the web, and found that you can only use lookup fields on foreignkey related objects, so my question is how can you query objects by file field url if thats the case ? It seems like this would be a very common query performed.
This is what I get when I do a simple query on media_file with icontains
In[27]: MediaLibraryFile.objects.all()[0].media_file.url
Out[27]: '/media/2017/6/13/444e35c2-0432-479a-805d-c46638ee3600.jpg'
In [28]: MediaLibraryFile.objects.filter(media_file__contains='/media/2017/6/13/444e35c2-0432-479a-805d-c46638ee3600.jpg')
Out[28]: <QuerySet []>
If you know exactly what you want to match you could do;
MediaLibraryFile.objects.filter(media_file__exact='some_key')
Or alternatively you could do a query like;
MediaLibraryFile.objects.filter(media_file__contains='some')
Just beware, the above is case sensitive. If you don't know the case, you can do filter(media_file__icontains='something')
The docs for field lookups are here; https://docs.djangoproject.com/en/2.0/topics/db/queries/#field-lookups

TypeError: expected string or bytes-like object User.id

I'm trying to register an new Transaction object on the DB using Django, but I'm having TypeError: expected string or bytes-like object when I try to do user_id = user.id I can't really understand why this is happening, since I do the same steps when registering a new Bank object (as shown on prints below). I've tried to debug and the local variables have the correct value, I've also tried to cast user.id with string or int, but none of them worked.
traceback console error create Transaction method create Bank method
models.py
Firstly, please don't post code or errors as images; they are text, they should be posted as text in the question.
However I don't see anything in any of those snippets that suggest the error is with the user - that line is probably highlighted because it's the last in that multi-line call.
Rather, the error looks to be in the reference to date.today - if that's the datetime.date class, then today is a method, which you would need to call:
Transaction.objects.create(date=date.today(), ... )
Or, since that field has a default anyway, you could leave out the date attribute from the create call altogether.

Django unable to save a text value in DB

I'm reading an e-mail content through IMAP in my Django app.
When I try to assign some of the parsed content to the object and do .save() it returns:
ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
When I print the variable type: . Field in the DB is defined as CharField. I tried TextField as well, but the result is the same.
How I can solve that?
if your mail text is in mail_text, do this:
mail_text = unicode(mail_text)

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.

Invalid keyword argument on new model entry

I have the following model:
class mark(models.Model):
title=models.CharField(max_length=35)
url=models.URLField(max_length=200)
user=models.ManyToManyField(User,blank=True)
and then I use a form to save some data to the db. My code inside the view that saves the data is:
new_mark= mark(url=request.POST['url'],
title=request.POST['title'],
user=request.user)
new_mark.save()
Of course I have all the data validation, login required validation, etc.
When I run this it throws me an unexpected
'user' is an invalid keyword argument for this function
on theuser=request.user) line. Any ideas what might be wrong?
Please provide the whole traceback and make sure your view has no function named "mark" etc (You probably also want to change mark to Mark to follow Python and Django style guides.) test via print type(mark) before the "new_mark = …" line.
Also I am not 100% sure if a ManyToMany field allows settings data like that, eg try:
new_mark= mark(url=request.POST['url'],
title=request.POST['title'])
new_mark.user.add(request.user)
new_mark.save()
And since it's an m2m field you probably want to rename the field to users.