2

I'm building a custom function to pull the data from mongodb and want to see the data what it is look like before the manipulation.

say I have a function to request the data from mongodb

Here is how my models.py looks like

from bson import json_util
from django.db import models
from django.http import JsonResponse
from pymongo import MongoClient
from bson.json_util import dumps
from bson.json_util import loads
import json
import pymongo

def data_pull(request):
     request_data = request.POST.get('Hello', None) 

if __name__ == "__main__":
    data_pull(request)

to run the models.py I do python models.py from the command line but getting this error

NameError: name 'request' is not defined

So basically I want to test this data_pull function from models.py and see that what the data looks like. How can I feed this request call into the function and see the output from commandline?

4
  • The HTTP request usually only exists in the views (in the context of the HTTP request-response cycle) Commented Nov 9, 2021 at 19:16
  • How do you call the function? If from a view, then you can pass the request as a parameter to the function Commented Nov 9, 2021 at 19:17
  • @Ralf thank for response. Yes I'm calling it from views.py Commented Nov 9, 2021 at 19:22
  • @Ralf do you mean if just to python views.py call with if __name__=='main' at the bottom of views.py I should be fine ? Commented Nov 9, 2021 at 19:24

1 Answer 1

2

This part of your code seems strange:

if __name__ == "__main__":
    data_pull(request)

Usually, if you pass a request as argument, you would call the function data_pull() from a view and pass the request as an argument in the function call.

You cannot just use if __name__ == "__main__": and expect a request object to appear, you need to create one yourself or use the request object that is created by the views (the django engine takes care of if for the most part).

More info in the docs: https://docs.djangoproject.com/en/3.2/ref/request-response/#quick-overview


EDIT: if you want a HttpRequest object, that usually means that you want the data that is sent from a webbrowser (the data that the django engine places inside request.GET and request.POST and others). That means that you should probably call you function data_pull() from a view.

For example this code in your 'views' file (code from https://docs.djangoproject.com/en/3.2/topics/class-based-views/intro/#using-class-based-views):

from django.http import HttpResponse
from django.views import View

# since you function is declared in 'models', we import it here
from .models import data_pull

class MyView(View):
    def get(self, request):
        # call the function passing request as argument
        data_pull(request)

        return HttpResponse('result')
Sign up to request clarification or add additional context in comments.

2 Comments

Can you give more intuituve picture what needs to be done based on the original question asked. I tried to call the function in views.py to make a request from models.py but still it did not worked. Can you explain what did you mean about you need to create one yourself or use the request object that is created by the views ?
@Alexander ok, I tried to explain it differently

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.