Watch This Before Using Django With MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everyone so in this video i want to talk to you about using mongodb with django so on one hand you have an awesome python based web framework with django it's probably the number one framework by a lot of standards and on the other side you have a nosql document oriented database in mongodb insanely popular and definitely one of the better tools to use out there so naturally a lot of people want to use these two together so i want to take a look at if this is even possible if it does work what are the trade-offs and should you go this route so with this video i do have an article that i just wrote up about 10 minutes ago i just finished editing it and i'm going to publish it by the time the video is out and it's going to be linked up in the video description so check it out because i am referencing that article right now so let's go ahead and get into it and with that article i start off with a question can i use mongodb with django and the answer to that is yes but not in the way that you think so i really want to emphasize that because there are a lot of trade-offs here so let's go ahead and start with talking about mongodb in python and i'll kind of lead into it and talk about packages that come with this when it comes to using mongodb with python there is actually a package that's supported by mongodb called pymongo and this is a great tool to actually use python to connect to a mongodb database so you can connect to it read and write to that database and you have full access to that database so it actually works very well and i've used this with lighter python based web frameworks like fast api and flask and they work well together because these lightweight frameworks are not really opinionated in how they want you to build out your application so typically you'd use something like sql alchemy for your orm to actually connect to that database and then make queries but you don't actually have to do it so it's not built around it they really don't tell you how to work with it so pymongo actually works very well with flask and fast api but with django this is a whole different story because django is a more opinionated framework it's an awesome framework so i always want to back it up that way but it does come with a lot of tools and because of that sometimes it doesn't play well with other tools so with django django is built by default for relational databases and it's really optimal for postgres but if you look in the django documentation it also really works well with mariahdb mysql oracle and sqlite and there are some other third-party databases that work with it so with that django is not really built for nosql databases just in the way everything functions these are the databases that it recommends now if you look up django and mongodb on youtube or in any articles you're usually going to see this package called jongo and there are a lot of issues with this so i really want to address this because i see videos teaching on it but no one really talks about the trade-off so i really want to address these first so with absolutely no disrespect to the creator of the jungle package because i'm sure they've worked hard on it i want to talk about the first issue here so the first one is the fact that jongo at this point is just an experiment it's a very underdeveloped package it helps you connect to the database here it actually helps you even migrate the database and make basic queries but beyond some basic crowd functionality there's really not much you can do with it so it's not practical for an actual application i try to create some relationships and try to scale this thing right away i started running into issues and then looking at the github repo i realized a lot of people were also presenting these issues and talking about the same problems so outside of just playing around with it it's not really practical it's extremely underdeveloped so i wouldn't recommend it for any practical use now the second issue is the fact that it's not supported it seemed like the creator had some fun with it and they kind of left it i actually did see a commit about 12 days ago so i'm surprised somebody actually did something with it because last time i saw before that was almost about two years ago and essentially in the package there are 298 open issues and people are having basic issues on just pretty simple queries with their database and they can't figure those out and no one's really going through that so the package really doesn't have much support and that's kind of a rule for me before i use anything it has to have support because if i run into an issue i'm using the package because i don't want to have to write my own code and hack it together i'm trying to use a package to help me out so it's extremely undersupported and the third issue that i have written in the article here is the fact that there's really no compatibility with later versions of django at this point the only version of django that i can get it to work with was django 3.0.5 and if you're using django you usually want to use some newer versions so that's kind of a pretty big issue for a lot of people the fact that it only works with a certain version of it and that's because the packages that it's using i did a lot of research on it are simply not compatible so no one's really modifying it and working with it so not too long ago i actually had a conversation with a developer advocate from mongodb you may know him as jessie or codestacker on youtube and he had this to say about the jongo package so we were actually working on trying to figure out some kind of tutorials that we can put together on using django with mongodb and we just ran into some roadblocks and basically decided that jungle was not the way so he said it seems that jongo is currently the only effective way to use mongodb with django unfortunately it is very much a hack and the repo seems to be sporadically maintained we don't have any contact with this maintainers now the main issue here is basically that it just converts sql queries to mongodb queries which really doesn't make sense for a document database you will not get any of the benefits of mongodb and it actually could result in poor performance in mongodb versus sql so he basically agreed with me he's talking about the functionality of mongodb and saying that it's basically not ready for practical use so in short we're trying to compare two very different types of databases and it's going to take a lot more work than what jongo has done with it now as far as coming up with something that does work it's definitely possible as far as connecting the two and jesse did have something to say about that so he said apparently we've been looking into we as in mongodb have been looking into django support for a while now but the built-in orm is very much tied to sql which makes it very difficult if you have any ideas we very much want to find a way to get into the django community so this is where we come in i'm definitely going to work on this i want to try to create some tutorials on this maybe even work on a package with a few people so if you have any ideas we would love your support so with that all hope is not lost i want to look at pie now so if you're still adamant about using django with mongodb and you want to test it the only practical solution i can see right now would be to use pi directly with django and there's going to be a lot of trade-offs here basically anything that has to do with the orm is going to be thrown out the window because it's not going to work anymore you're going to have to use pi directly and make queries to the mongodb database that way just using python and not the built-in django orm so until we have a package that directly ties the two it's just not going to work so if you're going this route these are the things you should consider i'm sure there's more but these are the first things that just come to the top of my head right now and that is going to be the fact that there's going to be no more orm any queries are going to be done with pymongo no more admin panel right now the admin panel is just too configured with the django orm by default the way the migrations work and everything connects with the user model all that authentication is done with a django orm and that's just probably not going to work if you can find a way cool but i don't see any way to do that right now with pymongo unless you hack an entire package together yourself as far as the database connection in settings.py where you have that database object and switch your databases that's no longer going to be a thing you're not connecting pi that way you're going to have to create your own configuration for the database and that's going to have to be done from scratch no more migrations no more calling the migrate command or make migrations that's not going to happen because there's no more automatic migrations with pymongo it's just not going to connect with django as far as authentication and models this is the part where i'm a little bit more unsure about because i need to look at how django's authentication system works in depth to see how i can actually have it work with pymongo but i do know we're going to lose a lot of access to that default user model and so on and as far as modeling our data that i'm not too sure about maybe you can use pydantic i haven't looked into it enough i do plan on doing a lot of work here so those are my main takeaways basically you're gonna have to ignore everything to do with the orm and rethink how django works we lose a lot of functionality django is still very useful outside of this but the orm is one of the most powerful features about django so it's sad that we're losing that that's why a package connecting these two would be absolutely amazing so i'm definitely going to be taking a look at using it if you have any ideas i would love to hear your feedback in the comment section i will be looking at making a video and using pie and django together definitely not jongo because that ship has sailed it's out the window unless someone comes in and really saves the day but i'm not going to hold my breath until that happens so i hope this video was informative i hope it gives you some ideas here and how this can work and i will see you all in another video
Info
Channel: Dennis Ivy
Views: 41,527
Rating: undefined out of 5
Keywords: Programming, Software Developer, Dennis Ivy, Dennis Ivanov
Id: ORog5kwPd_E
Channel Id: undefined
Length: 9min 17sec (557 seconds)
Published: Mon Aug 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.