doableware / djongo

Django and MongoDB database connector
https://www.djongomapper.com
GNU Affero General Public License v3.0
1.89k stars 355 forks source link

Filtering on PK -- matching query does not exist #438

Open aaroneden opened 4 years ago

aaroneden commented 4 years ago

Cannot get objects based on the primary key. -

View HTML

  <a href="{% url 'backtest_detail' backtest.pk %}"
                   class="btn btn-primary">
                    More Detail
                </a>

View Python

backtest = BackTestResult.objects.get(pk=pk)

Model Python

class BackTestResult(models.Model):
    _id = models.ObjectIdField(primary_key=True)
    cash_starting = models.DecimalField(max_digits=15, decimal_places=2)
    cash_ending = models.DecimalField(max_digits=15, decimal_places=2)
    ticker = models.CharField(max_length=6)
    strategy_name = models.CharField(max_length=30)

Traceback

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/backtests/5f0a7ca49de5c032d9773557/

Django Version: 3.0.5 Python Version: 3.7.3 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'backtests'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback (most recent call last): File "/Users/aaroneden/PycharmProjects/eStocks_Web/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/Users/aaroneden/PycharmProjects/eStocks_Web/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/Users/aaroneden/PycharmProjects/eStocks_Web/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, *callback_kwargs) File "/Users/aaroneden/PycharmProjects/eStocks_Web/backtests/views.py", line 17, in backtest_detail backtest = BackTestResult.objects.get(pk=pk) File "/Users/aaroneden/PycharmProjects/eStocks_Web/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(args, **kwargs) File "/Users/aaroneden/PycharmProjects/eStocks_Web/venv/lib/python3.7/site-packages/django/db/models/query.py", line 417, in get self.model._meta.object_name

Exception Type: DoesNotExist at /backtests/5f0a7ca49de5c032d9773557/ Exception Value: BackTestResult matching query does not exist.

AlejandroOrostica commented 4 years ago

Hi !

try this in your view

from bson.objectid import ObjectId

backtest = BackTestResult.objects.get(_id=ObjectId(pk))
aaroneden commented 4 years ago

That did the trick @AlejandroOrostica Thank you so much! Is there a way a sample about this could be added to the Djongo site?

AlejandroOrostica commented 4 years ago

I don't know where to find documentation about djongo :(, i knew the solution because i have a similar issue with pymongo a few weeks ago,

I'm working with djongo right now and i'm stuck filtering lists and don't know where to read about that :((((