Django Database query works on local machine, but not on server - django

This is the query:
comment = AdditionalComment.objects.get(form = form, student = students[student])
and works perfectly on my local machine.
Ive deployed the project to a DO VPS and whenever I try access the view i get this error even though I can see the AdditionalComment in the admin panel:
File "/home/smartsurvey/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/smartsurvey/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/smartsurvey/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/smartsurvey/venv/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/smartsurvey/smart-survey/forms/views.py", line 195, in view_replies
comment = AdditionalComment.objects.get(form = form, student = students[student])
File "/home/smartsurvey/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/smartsurvey/venv/lib/python3.6/site-packages/django/db/models/query.py", line 380, in get
self.model._meta.object_name
forms.models.DoesNotExist: AdditionalComment matching query does not exist.`
Any idea what it could be? let me know if you need anymore info

I realised I was querying the database for users comments before they had made them, so I added an if statement checking if they had replied.

Error says: there is no AdditionalComment with such parameters. You need create AdditionalComment instance, and after you can get it.

Related

django.db.utils.InterfaceError: connection already closed

Stack:
Ubuntu (20.04 LTS)
Nginx
Postgresql (v13.3)
An AWS load balancer sends traffic to the Ubuntu instance(k8s cluster), which is handled by Nginx, which forwards on to Django (4.0.3) running in gunicorn (19.9.0). Django connects to the database using psycopg2 (2.8.6).
The issue I have is that the database connection seems to shut down randomly.
Django reports errors like this:
`InterfaceError: connection already closed
File "django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "contextlib.py", line 79, in inner
return func(*args, **kwds)
File "django/views/generic/base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "django/views/decorators/cache.py", line 62, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "django/views/generic/base.py", line 119, in dispatch
return handler(request, *args, **kwargs)
File "django/views/generic/detail.py", line 108, in get
self.object = self.get_object()
File "django/views/generic/detail.py", line 53, in get_object
obj = queryset.get()
File "django/db/models/query.py", line 492, in get
num = len(clone)
File "django/db/models/query.py", line 302, in __len__
self._fetch_all()
File "django/db/models/query.py", line 1507, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "django/db/models/query.py", line 57, in __iter__
results = compiler.execute_sql(
File "django/db/models/sql/compiler.py", line 1359, in execute_sql
cursor = self.connection.cursor()
File "django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "django/db/backends/base/base.py", line 284, in cursor
return self._cursor()
File "django/db/backends/base/base.py", line 262, in _cursor
return self._prepare_cursor(self.create_cursor(name))
File "django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "django/db/backends/base/base.py", line 262, in _cursor
return self._prepare_cursor(self.create_cursor(name))
File "django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "django/db/backends/postgresql/base.py", line 256, in create_cursor
cursor = self.connection.cursor()`
Postgresql does not report any major errors: I can only assume that the connection was closed correctly and Django was not expecting it.
This happens quite rarely, but enough to be a concern: once every 3 days.
Any suggestions of how to investigate this further would be gratefully accepted. Thanks in advance!
I'm using django-service-objects and Django-Q for a distributed data mining code. I ran into a error "django.db.utils.InterfaceError: connection already closed" when I was running Django-Q cluster async_task(..., sync=True) to debug and attempted to query the Django PostgreSQL 13 DB using Model.object.get(pk=...).
I resolved the issue by setting db_transaction = False within the Service class.
class dataMining(Service):
db_transaction = False
https://django-service-objects.readthedocs.io/en/stable/pages/usage.html#database-transactions
The effect of db_transaction=True (enabled by default) is to "...process method on services ... inside a transaction". Meaning changes to the Django PostgreSQL DB are not actually saved until the def process(self) method of the Service class completes successfully. https://django-service-objects.readthedocs.io/en/stable/pages/usage.html#service
I suspect the self.object = self.get_object() call in your code sample is experiencing the same problem of being 'locked out' during a process method within a DB transaction.

I have a problem with get billings from paypal

I have a big problem with the webhook for confirms a subscribe agreements. I'm used the SDK [https://github.com/paypal/PayPal-Python-SDK/blob/master/samples/subscription/billing_agreements/get.py] If somebody could help me, my errors is the next:
Traceback (most recent call last):
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/django/core/handlers
/exception.py", line 34, in inner
response = get_response(request)
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/home/jjorge/src/guru/guru-payments/apps/paypal/views.py", line 69, in post
settings.PAYPAL_CLIENT_SECRET
File "/home/jjorge/src/guru/guru-payments/apps/paypal/services.py", line 34, in execute
paypal_secret_id
File "/home/jjorge/src/guru/guru-payments/apps/paypal/payment_methods.py", line 154, in get_billing_agreement
'client_secret': paypal_client_secret
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/paypalrestsdk/resource.py", line 110, in find
return cls(api.get(url, refresh_token=refresh_token), api=api)
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/paypalrestsdk/api.py", line 268, in get
return self.request(util.join_url(self.endpoint, action), 'GET', headers=headers or {}, refresh_token=refresh_token)
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/paypalrestsdk/api.py", line 171, in request
return self.http_call(url, method, data=json.dumps(body), headers=http_headers)
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/paypalrestsdk/api.py", line 214, in http_call
return self.handle_response(response, response.content.decode('utf-8'))
File "/home/jjorge/venvs/payments/lib/python3.7/site-packages/paypalrestsdk/api.py", line 231, in handle_response
raise exceptions.ResourceNotFound(response, content)
paypalrestsdk.exceptions.ResourceNotFound: Failed. Response status: 404. Response message: Not Found. Error message: {"name":"RESOURCE_NOT_FOUND","debug_id":"9a7aa1a765763","message":"The requested resource was not found","information_link":"https://developer.paypal.com/docs/api/payments.billing-agreements#errors","details":[{"issue":"Requested resource ID was not found."}]}
Deprecation notice: The /v1/payments/billing-agreements endpoints are deprecated. Use the /v1/billing/subscriptions
That is my solution, I had to implement a new function with the new api
class PayPalPaymentMethod:
#staticmethod
def get_billing_subscription(billing_agreement_id, paypal_mode, paypal_client_id, paypal_secret_id):
url = util.join_url('/v1/billing/subscriptions', billing_agreement_id)
api = Api({
'mode': paypal_mode,
'client_id': paypal_client_id,
'client_secret': paypal_secret_id
})
result = api.get(url)
return result

why swagger raises unclear error - Django

I have a django rest Backend app, and i use swagger to look and document my apis to the FE.
This worked fine, but I made some changes and now I get this error:
Internal Server Error: /
Traceback (most recent call last):
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/rest_framework/views.py", line 497, in dispatch
response = self.handle_exception(exc)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/rest_framework/views.py", line 457, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/rest_framework/views.py", line 468, in raise_uncaught_exception
raise exc
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/rest_framework/views.py", line 494, in dispatch
response = handler(request, *args, **kwargs)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/rest_framework_swagger/views.py", line 32, in get
schema = generator.get_schema(request=request)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/rest_framework/schemas/coreapi.py", line 153, in get_schema
links = self.get_links(None if public else request)
File "/home/notsoshabby/.local/share/virtualenvs/panda_pitch-UBt5SNMA/lib/python3.7/site-packages/rest_framework/schemas/coreapi.py", line 140, in get_links
link = view.schema.get_link(path, method, base_url=self.url)
AttributeError: 'AutoSchema' object has no attribute 'get_link'
HTTP GET / 500 [0.15, 127.0.0.1:44214]
/home/notsoshabby/Desktop/panda_pitch/django_project/settings.py
This error is not very clear as the AutoSchema is not a part of my code and the traceback is not showing me where in My code the problem is.
I made too many changes to go one by one and check which one caused that.
Anyone experienced this issue before? Any ideas on how to debug to find which change causes this issue?
I ran into the same issue, the fix is described here: https://www.django-rest-framework.org/community/3.10-announcement/
To summarize, Django Rest Framework 3.10 (released a few days ago) deprecated the CoreAPI based schema generation, and introduced the OpenAPI schema generation in its place. Currently to continue to use django-rest-swagger as is you need to re-enable the CoreAPI schema generation by adding the following config to the settings file:
REST_FRAMEWORK = { ... 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema' }

build_filters() got an unexpected keyword argument 'ignore_bad_filters'

I just updated my code from django 1.8 to 1.11 and so i also had to update tasty pie. Before update everything worked fine but now I am getting this error on one of my tests.
Here is the test case
def test_does_not_return_restricted_fields_when_insufficient_permissions(self):
self.user.user_permissions.all().delete()
response = self.api_client.get(url,
format='json',
authentication=self.create_apikey(username=self.username, api_key=self.api_key),
data=data)
Here is the full Trace,
Traceback (most recent call last):
File "/spare/local/projects/towerportal/apps/exchangedb/tests/test_api.py", line 68, in test_does_not_return_restricted_fields_when_insufficient_permissions
response = self.get_response(self.detail_url)
File "/spare/local/projects/towerportal/apps/exchangedb/tests/test_api.py", line 26, in get_response
data=data)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/test.py", line 70, in get
return self.client.get(uri, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/django/test/client.py", line 536, in get
**extra)
File "/spare/local/venv/lib/python2.7/site-packages/django/test/client.py", line 340, in get
return self.generic('GET', path, secure=secure, **r)
File "/spare/local/venv/lib/python2.7/site-packages/django/test/client.py", line 416, in generic
return self.request(**r)
File "/spare/local/venv/lib/python2.7/site-packages/django/test/client.py", line 501, in request
six.reraise(*exc_info)
File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 221, in wrapper
response = callback(request, *args, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 470, in dispatch_detail
return self.dispatch('detail', request, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 493, in dispatch
response = method(request, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 1376, in get_detail
obj = self.cached_obj_get(bundle=basic_bundle, **self.remove_api_resource_names(kwargs))
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 1195, in cached_obj_get
cached_bundle = self.obj_get(bundle=bundle, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 2176, in obj_get
applicable_filters = self.build_filters(filters=kwargs, ignore_bad_filters=True)
TypeError: build_filters() got an unexpected keyword argument 'ignore_bad_filters'
Could your please paste your tastypie resource code?
Without related code, I can only guess that it is because you override
def build_filters(): in your modelresource class, but the newer version of tastypie added a ignore_bad_filter parameter in the function definition which you do not have in your modelresource code.
The build_filters function declaration in the latest tastypie:
def build_filters(self, filters=None, ignore_bad_filters=False):
The old version probably does not have the ignore_bad_filter parameter. To fix this, just add ignore_bad_filters=Falsekey word parameter to the build_filters function in your modelresource class.

AssertionError: Negative indexing is not supported

When i open to click cms-pages , showing error..
File "/home2/devnocco/virtual/lib/python2.6/site-packages/Django-1.3.1-py2.6.egg/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home2/devnocco/virtual/lib/python2.6/site-packages/Django-1.3.1-py2.6.egg/django/contrib/admin/options.py", line 307, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home2/devnocco/virtual/lib/python2.6/site-packages/Django-1.3.1-py2.6.egg/django/utils/decorators.py", line 93, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home2/devnocco/virtual/lib/python2.6/site-packages/Django-1.3.1-py2.6.egg/django/views/decorators/cache.py", line 79, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home2/devnocco/virtual/lib/python2.6/site-packages/Django-1.3.1-py2.6.egg/django/contrib/admin/sites.py", line 197, in inner
return view(request, *args, **kwargs)
File "/home2/devnocco/virtual/lib/python2.6/site-packages/django_cms-2.2-py2.6.egg/cms/admin/pageadmin.py", line 657, in changelist_view
cl.set_items(request)
File "/home2/devnocco/virtual/lib/python2.6/site-packages/django_cms-2.2-py2.6.egg/cms/admin/change_list.py", line 198, in set_items
children[-1].last = False
File "/home2/devnocco/virtual/lib/python2.6/site-packages/Django-1.3.1-py2.6.egg/django/db/models/query.py", line 157, in __getitem__
"Negative indexing is not supported."
AssertionError: Negative indexing is not supported.
This will happen mostly when you are trying some indexing related operation on an empty list. Try this instead:
somelist=[]
last_element = somelist[len(somelist)-1]
It looks like you are coming across this issue in django-cms. It's been fixed now, so you could upgrade to the latest release, or manually apply the patch.
You might have to upgrade django-mptt to 0.5.2 at the same time.