I have written a web app in django using mongoengine as a driver and am trying to host my database on mongodb cloud. In mongoengine's connect() method, according to what I understood from the documentation, specifying the url (specified in the connect section on the mongo cloud site) in the hostname argument should do the trick, however requests from the app to the mongo cloud keep timing out.
Where am I going wrong?
This is the call to the connect method in my setting.py file-
mongoengine.connect(db='scheduler', host='mongodb+srv://<user>:<password>#cluster0.oualt.mongodb.net/<db name>?retryWrites=true&w=majority')
Update: the specific error thrown on using the above url is this-
ServerSelectionTimeoutError
If you're using just mongoengine then Django will not detect that connect to the database, so you will not be able to use the Django ORM system to save instances of models etc.
If you want to use the ORM you would have to use a package like Djongo or djmongoengine
However, if you're not planning to use the ORM system then you can use just mongoengine.
I believe the connect url should be in this format:
mongodb+srv://<username>:<password>#<database-name>.mongodb.net/test?retryWrites=true&w=majority
Right now you seem to have the url to a specific cluster on your database.
So I was going through similar posts which had the same error ServerSelectionTimeoutError with djongo and pymongo as the drivers and one of the posts said that they had forgotten to whitelist their IP address on the mongo cloud site. I had done that during signup, but I repeated it anyways and for some reason the IP address was different and after that had been whitelisted, my app connected and started working. I have no explanation.
mongoengine works fine, but in terms of protocols you need another ext file that serves for your connection protocols, just pip install pymongo[srv]
def register_database(db_name:str):
connect(db=f"mongodb+srv://<username>:<password>#<cluster-name>.nwr5l.mongodb.net/{db_name}?retryWrites=true&w=majority", alias="default")
Related
I want my database deployed on sqliteonline instead of django admin but i am stuck at how to do that. Thanks in advance. It will be of much help!
I hope you are keeping well.
I believe from my understanding that you are misinterpreting what SQLITEONLINE is. It is simply a web-based database IDE.
If you want to host your database remotely for Django, I would recommend changing from the Django default of sqlite as this is a database designed for local storage and not remote connectivity.
To change your database to a remote one, you could go to a SQL database and connect remotely by adjusting the settings in your settings.py file. See Django's documentation on the topic here.
I want to make a project, which uses Django as backend, PostgreSQL as database and FastAPI with Django REST Framework for REST.
Don't see any problems with making a project just with Django, DRF and Postgres, but face with difficulties when speak about FastAPI and DRF at the same time.
So there is no problem in connecting Postgres to Django, and there is no problem to make endpoints for DRF.
But how can I connect fastapi? Where to place endpoints and how to run all this stuff together?
In some examples I saw that FastAPI isntance is initiated in WSGI.py and then server runs from calling commands such like this:
uvicorn goatfish.wsgi:app
But i am not sure that it works like this when I mix more than only Django and FastAPI.
I want to use FastAPI for optical character recongnition and DRF for user registration, logins etc.
Are there any advices about making project with such a structure? Or maybe someone have a repository with such kind of project on github?
EDIT: Hope to see answers here, but for now I only see the solution in making classic Django + DRF app, then make FastAPI app with endpoints, run these apps on different ports and do some chain of actions:
From django app we load an image to form and when we submit this form we send POST request to FastAPI endpoint, which will run OCR process and then return JSON with recognized text and then will send this JSON to the Django Callback endpoint, which will process it and save to the database.
What do you think about such thing?
I think, you may:
Mix of fastapi+django. But this is only for replace DRF and use fastapi as rest framework.
Microservices. You may to run Django on one port, fastapi - another. And both may to use one shared database.
Mircoservices. All from point 2, but some api tasks (for example sign-in/sign-up) on Django and another - on fastapi.
As an alterantive you could use django ninja.
It uses similar concepts as FastAPI (Routes, Pydandantic Model Validation, Async Support) but is an Django app.
Which of course is more the monolithic approach but still valid.
Well, so after few days of thinking about I decided, that there is no sense in a question which I asked :)
Here we should talk about microservice architecture, where such kind of problem just doesn't exist. All we need is to make as much services as we need in our project using any framework (Django, FastAPI, Flask, Ruby etc.) and make connections between them.
As example I can run my main Django server on port 8000, my FastAPI server on port 5000 and my DRF service on port 6000. And then I can just do whatever I want from my main Django server making requests to endpoints of FastAPI and DRF.
So that's very simple example, now I'm diving deeper into microservice architecturing, but that's definitely what I need.
I used the answer to this question to fix a circular import when setting up Flask-SQLAlchemy. The solution works because you don't have to import the app from the application.py file to get the database URI - it initalizes the connection just before running the app. The issue now is that I typically like import my models file into a python interpreter just so I can test the models (and the connection to the database) without actually running the app. But with this setup, just importing models won't establish a connection to PostGres.
Is there a way to establish a connection to PostGres through Flask-SQLAlchemy without actually initializing and running an app? Maybe I can provide db with the URI to my database directly?
Study this code
https://github.com/mattupstate/overholt
there is a link on github explaining the design. What should be your main focus is the usage of flask-script. I think you have to use the factory pattern, but you want to do that anyway if you are doing anything remotly serious.
Best of luck
It looks like you are searching for this one github.com/mardix/active-alchemy
Active-Alchemy is a framework agnostic wrapper for SQLAlchemy that makes it really easy to use by implementing a simple active record like api, while it still uses the db.session underneath. Inspired by Flask-SQLAlchemy
I am doing simple app which has to connect to external REST api, get data in json and print it for the user. My questions are:
Where should I put/create a module which will connect to external REST api? I mean I could just write some code in views.py which connects to REST api and the passes the results to template but I want to separate it in some autonomic module which I could use in views.py like myapimodule.get_devices() which for example will connect and get data from example.com/api/device/get. I tried python and django for first time today so I just want to know where u put (and how) such modules in django app?
How I can connect to RESTAPI with django? I have for example username and password for http auth and address like example.com/api/device/get - what parts of django lib will allow me to use the restapi and retrieve data from it (in json format)?
There's really no "right" answer. It just depends on what's best for your needs.
To connect to an external REST API, take a look at the excellent Requests library.
The Requests library is worth learning. It will save you a lot of grief.
Did you try:
pip install chardet
before
pip install requests
?
I need to use the mysql backend in Django. Documentation in the settings file says just use 'django.db.backends.mysql', and this is also documented at https://docs.djangoproject.com/en/dev/ref/settings/#engine
When I set a database setting engine to 'django.db.backends.mysql' Django errors out.
I've found references on the web that one needs to install MySQLdb. However, I look in my site-packages where I find django/db/backends/sqlite3 and django/db/backends/mysql.
So, it looks like I have the Django back ends in place. Do I have the MySQL back end installed but have a configuration problem, or do I need to install the MySQLdb package to allow the backend to work?
django.db.backends.mysql is a wrapper around MySQLdb; you will need to have MySQLdb installed for it to work, or it will raise an ImproperlyConfigured exception on startup.
The Django database backends generally work like this -- they rely on a lower-level database interface library, but they provide a uniform interface to the ORM layer.
The only backend that will work "out-of-the-box" is SQLite, but that backend is no different, it's just that the SQLite interface is built into the python distribution, so it's almost guaranteed to be present.
You will have to install MySQLdb in order to use MySQL as db backend.
If you read the documentation, it states,
If you plan to use Django's database API functionality, you'll need to
make sure a database server is running.
So yes, you need to install a MySQL server in order for the backend to work.