django-1.3

Executing raw SQL against SQLite with Django results in `DatabaseError: near “?”: syntax error`

不羁岁月 提交于 2019-12-02 14:23:20
问题 For example, when I use cursor.execute() as documented: >>> from django.db import connection >>> cur = connection.cursor() >>> cur.execute("DROP TABLE %s", ["my_table"]) django.db.utils.DatabaseError: near "?": syntax error When Django's argument substitution is not used, the query works as expected: >>> cur.execute("DROP TABLE my_table") django.db.utils.DatabaseError: no such table: my_table What am I doing wrong? How can I make parameterized queries work? Notes: Suffixing the query with ;

Saving a decoded temporary image to Django Imagefield

我们两清 提交于 2019-11-30 12:21:15
问题 I'm trying to save images which have been passed to me as Base64 encoded text into a Django Imagefield. But it seems to not be saving correctly. The database reports all my images are stored as "" when it should report them as a filename for example: "template_images/template_folders/myImage.png" The code that's trying to save my images is as follows: elif model_field.get_internal_type() == "ImageField" or model_field.get_internal_type() == "FileField": # Convert files from base64 back to a

Django Class-Based Generic Views and Authentication

北慕城南 提交于 2019-11-30 08:35:58
问题 I am pretty new to Django (starting with 1.3). In building an app, I went with the new class-based generic views from day one, using a combination of the built in classes and subclassing them where I needed to add to the context. Now my problem is, I need to go back to my views, and have them accessible only to logged in users. ALL the documentation I have found shows how to do this with the old functional generic views, but not with class-based. Here is an example class: class ListDetailView

Django aggregate queries with expressions

末鹿安然 提交于 2019-11-30 04:23:35
问题 I have a model XYZ and I need to get the max value for fields a, b, and expression x/y for a given queryset. It works beautifully for fields. Something like: >>> XYZ.all().aggregate(Max('a')) ... {'a__max': 10} However, I can't find a way to do it for expressions. Trying something like: >>> XYZ.all().aggregate(Max('x/y')) Gives an error: *** FieldError: Cannot resolve keyword 'x/y' into field. Choices are: a, b, x, y, id Trying something like: >>> XYZ.all().aggregate(Max(F('x')/F('y'))) Gives

Saving a decoded temporary image to Django Imagefield

ぃ、小莉子 提交于 2019-11-30 02:29:35
I'm trying to save images which have been passed to me as Base64 encoded text into a Django Imagefield. But it seems to not be saving correctly. The database reports all my images are stored as "" when it should report them as a filename for example: "template_images/template_folders/myImage.png" The code that's trying to save my images is as follows: elif model_field.get_internal_type() == "ImageField" or model_field.get_internal_type() == "FileField": # Convert files from base64 back to a file. if field_elt.text is not None: setattr(instance, model_field.name, File(b64decode(field_elt.text))

What is the easiest way to clear a database from the CLI with manage.py in Django?

一个人想着一个人 提交于 2019-11-29 22:45:28
I am using Django to build a website with MySQL. Now as I am learning so I need to change the Model very often so I want that all tables get cleared and new table get created. But syncdb doesn't touch existing tables. Is there any better way to handle this problem? manojlds If you don't care about data: Best way would be to drop the database and run syncdb again. Or you can run: For Django >= 1.5 python manage.py flush For Django < 1.5 python manage.py reset appname (you can add --no-input to the end of the command for it to skip the interactive prompt.) If you do care about data: From the

How do I handle file upload via PUT request in Django?

偶尔善良 提交于 2019-11-28 23:15:20
I'm implementing a REST-style interface and would like to be able to create (via upload) files via a HTTP PUT request. I would like to create either a TemporaryUploadedFile or a InMemoryUploadedFile which I can then pass to my existing FileField and .save() on the object that is part of the model, thereby storing the file. I'm not quite sure about how to handle the file upload part. Specifically, this being a put request, I do not have access to request.FILES since it does not exist in a PUT request. So, some questions: Can I leverage existing functionality in the HttpRequest class,

How do I handle file upload via PUT request in Django?

痴心易碎 提交于 2019-11-27 14:39:26
问题 I'm implementing a REST-style interface and would like to be able to create (via upload) files via a HTTP PUT request. I would like to create either a TemporaryUploadedFile or a InMemoryUploadedFile which I can then pass to my existing FileField and .save() on the object that is part of the model, thereby storing the file. I'm not quite sure about how to handle the file upload part. Specifically, this being a put request, I do not have access to request.FILES since it does not exist in a PUT