Django objects.all() empty query set, not empty in shell - python

I have been using Django for the past 2 years with no issues at all, I am no rookie.
I recently created a new attribute for a model and added it to the mysql database.
Whenever I filter things using this attribute in my "views.py" i get an empty set, whenever I use the same exact line of code using "manage.py shell" it returns the expected queryset.
I have tried restarting the httpd service, the mysqld service and I have rebooted the system but nothing happens.
Does anyone know how to bypass this issue?
Here's the code I used to debug this issue:
Shell:
It won't let me post pictures so here's the code:
>>>Session.objects.filter(cancellation_session=True)
>>>[<Session: Zumba Toning (Ladies)>]
views.py:
def gym_day2(request):
return HttpResponse(str(Session.objects.filter(cancellation_session=True)))
HttpResponse:
[]
Please Help!

add . values or .values_list() after the statement , Session.objects.filter(cancellation_session=True).values(´field´)

Related

Django Admin ERR_QUIC_PROTOCOL_ERROR

Django Admin list view page for a single model returns a blank page. it only load some of the buttons. console shows this error Failed to load resource: net::ERR_QUIC_PROTOCOL_ERROR. Have been trying somethings but couldn't solve this one. Also it doesn't happen on local machine. I am using django jet. tried using the vanilla django admin but still the same error. Other than this model all the other model's list view page is working fine.
If anyone has faced this error in the past or know anything about it, suggest me some fixes. any help would be appreciated. Thanks

Django: psycopg2.errors.UndefinedColumn: column "page_image" of "pages_page" relation does not exist

Here is a brief backstory. I am using the Mezzanine CMS for Django. I created some models that inherited from the Mezzanine models. This caused an issue in my Postgres database, where one object was present in two tables. When I would try searching my site for a post, I would not get results from one table.
So, here is where I believe I messed up. I reverted my models to how they were before this issue. This meant that there was still a table in my database for those models, so my search function still wouldn't work. I wanted this relation gone, so I did something very stupid and deleted the entire database. This was fine for my local development, because I just recreated the database and migrated.
When I try deploying this project of mine with the newly created postgres database onto DigitalOcean, I get to this command:
$ python manage.py createdb --nodata --noinput which gives me the error:
psycopg2.errors.UndefinedColumn: column "page_image" of "pages_page" relation does not exist
LINE 1: UPDATE "pages_page" SET "page_image" = '', "keywords_string"...
I looked up a few threads on this error, and tried this solution that did not work:
python manage.py shell
>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()
I have also tried the suggestion in the error LINE 1: UPDATE "pages_page" SET "page_image" = '', "keywords_string".... The weird thing was, I saw the page_image column inside the pages_page table before running this command.
I do not know what code may be helpful in this case. I do not know how to troubleshoot this issue. How can I further troubleshoot this issue of mine?
I figured this issue out. It has something to do with the EXTRA_MODEL_FIELDS option in the settings.py file. I'm still not sure why that causes an issue, but here is my EXTRA_MODEL_FIELDS code:
EXTRA_MODEL_FIELDS = (
(
...,
(
"mezzanine.pages.models.Page.page_image",
"ImageField",
(_("Featured Image"),),
{"blank": True, "upload_to": "uploads/blog"},
),
...
)
What I did that caused my issue to go away is as listed below:
I deployed with the createdb --nodata --noinput command commented out.
I continued the deployment process
Once I got to deploying the website, I just ran migrations again
The page_image column was automatically added
Now everything works fine! I don't know how much help this answer might be, but I can always go more into depth if someone wants to chat about their issues.

How to Running external command (Django) for overriding files in server?

After modified inspectdb, I want to run it when index page on web is loaded. So, in my view.py - def index, I´m trying to do the next:
def index(request):
subprocess.Popen("rm /path/app/models.py", shell=True)
subprocess.Popen("python2.7 /path/manage.py inspectdb_New > /path/app/models.py", shell=True)
return render_to_response('index/index.html', context_instance = RequestContext(request))
That is not working. I tried with os.system, subprocess.call as well, but it´s still not working.
For me, at least, looks like I can´t modify models.py in execution time but I don´t know what could be the problem...
Any idea guys?
Thanks.
You can't change your models.py while the webserver is running, because models (and other python code) are only loaded at server startup.
There are 2 parts in you question:
a) you want the shell to do something when view function is called. I have alarm beeping already.
b) you want to load new model. It won't gonna happen. You must reload to have new code loaded in (model introspected, etc.). Probably you use runserver command, take a look on Django autoreloader code: https://github.com/django/django/blob/master/django/utils/autoreload.py
It tracks files listed by gen_filenames() and reload when something was changed. You model was not listed so the change to the code will not be reflected.
I would touch something or remove .pyc files to force the reloader.

no such table: pages_page

I'm getting the error above when setting up my website with apache &mod_wsgi.
I've gone through all of the documentation managed to it half working. Here is the full error:
Exception Type: DatabaseError
Exception Value: no such table: pages_page
I know there is nothing wrong with the DB as before i set the site up with mod_wsgi i could run the site with ./manage.py runserver and it would all work fine.
I have to be carefull what code I post for security perposes.
More than happy to post more information if you tell me you need but i don't really know what to post up as the site itself works fine.
Got it working. I was missing the DJANGO_SETTINGS_MODULE variable from the settings.py file so the apache file was looking for it, but it didn't exist.
Thanks for your replies
There is something wrong with the DB.
When given a nonexistent file name, SQLite will create a new, empty database; and when given an empty file name, SQLite will create a temporary database.
Check that the database file name is configured correctly, and that it's accessible by the web server.

Blank responses from Django runserver when checking user permissions

I have a similar problem to those listed in
Django produces blank pages when POST data is sent and Models are accessed
and
Nginx connection reset, response from uWsgi lost
Here is one of the views in question:
#transaction.commit_on_success
#occ_update
#checks_status
def hold(request):
if not request.user.has_perm('orders.hold'):
return error_response_rollback(NO_PERMISSION_MSG % "hold orders")
order = Order.objects.get(pk=request.POST.get('pk'))
occ_revision = int(request.POST.get('occ_revision'))
agent = Agent.get_agent(request.user)
action = Action(agent=agent, type='hold_order',
comments=request.POST.get('comments'))
action.save()
order.hold(action, occ_revision)
return ok_response_commit("Order held successfully.")
error_response_rollback rolls back the transaction and returns an HttpResponse with JSON as its contents.
I am adding permission checking to many of my views in my application and when the user does not have the correct permission, a blank response is returned.
However like the questions referenced above, if you put a
print request
or
request.POST
statement BEFORE the permission check, the NO_PERMISSION_MSG JSON string is returned to the browser correctly every time (error_response_rollback returns an HttpResponse object with JSON in it.)
You get blank responses when you check permissions before the "print request" and they do not have the correct permissions.
You do NOT get blank responses when:
the user has the correct permissions
a "print request" statement is before any permission check
you use Firefox at any point.
The #occ_update and #checks_status decorators just catch exceptions. These problems occur with and without them present.
I'm developing in Chrome and none of this is an issue in Firefox.
One page I found suggested overloading the WSGIRequest object to read the request before it is passed to the view but this seems icky to me and I'd rather find out the real solution.
Does anyone know of any fixes/settings to the runserver command to help this issue without hacking on the request? My users are primarily using Chrome so I'd prefer to keep using it... we'll see. Currently developing in Windows using Django 1.3.1
One option I have considered is making another manage.py command to handle this but that, too, seems icky.
Thanks
Update:
I have been able to re-organize my code so that any permission checks happen after some bit of data is read from the POST. This seems to have eliminated any symptoms of this problem. It's still not ideal but it is a good alternative to inserting middleware to read the post. and won't always be possible in all applications.
Please comment if you have a similar situation and just can't figure it out.
As saying in the second link in your post, especially http://forum.nginx.org/read.php?2,196581 : when you works w/ Nginx and uWSGI and get a non-empty POST, always read the request.POST before return an HttpResponse. The reason is described in the link.
You don't have to override an handler, just put the request.POST line before the return code, or inside some decorator or middleware.
I encountered this issue for production site half a year ago and put the line in a middleware to solve it.

Categories