[LIVE] Django with MongoDB: Building and app with Python and Djongo

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to another stream if you're watching right now go ahead and say hi in the chat today i'll be working on another django application and this is something that's been requested and that i've wanted to do for a while and i actually haven't done it before so i almost always use postgres with django actually i've pretty much always used it except some test taps with sql light and stuff and even you know whether i make a rest api or a graphql api or anything i'm always using you know postgres as the database but this time i want to use mongodb so i've done a little research in preparation to figure out what are the best tools to use mongodb or to be able to interface mongodb with a django application and so i've included these in this wiki repo that i've been building for different ways of setting up django applications so i'm going to go down if you look in the table of contents i added this django section so i'm going to go here and i looked at the jungle library it seems to be the by far the most robust option and also what most people use so i'm going to go with the jungle jungle jungle mongodb library to build this django application now it will be a django rest framework application and it will hook up into mongodb for the database using this tool that's such a such a stark contrast going from you know dark themed pages to light themed pages anyway um yeah so i'm gonna get started there i have not started my uh django application yet so i'm going to go ahead and start that and i'm going to be going through a general setup for all jingle projects but i'm going to skip a couple things to make this faster so i'm going to skip the part where you know i always make a custom user but in this case oh i just realized i have a typo here but in this case i'm not going to make a custom user because i just won't have users now um let me stay up here so what am i going to be building so if you joined my streams before you might remember i have this spaced repetition application i have a vue.js front end but i have a django back end so that is with django rest framework and this one is with a django graphql and now i am going to be making one with mongodb this time so let me get started um first of all i use poetry as my package manager so i'll be using that postgres i'm going to ignore that for now and i need to start a django project so yeah i guess now that i'm trying out i need to get rid of postgres as i need a better structure for this readme so if anyone has any ideas let me know okay so i'm going to start a jingle project first by doing uh let's see django admin start project and then what should i name this project um i guess i'll call it what did i call my repo django srl yeah i guess that's fine uh srl stands for spaced repetition learning so i'll do django srl and then that created that directory so let me cd into that and now let me open that in vs code by the way i have in the background here you can see i have mongodb compass open because i'll be using that um just as a client to look at my db my collections and stuff okay so i have my main folder i have the manage.pi file now i am going to change this because as you may know already it's kind of weird to have django srl as the parent folder and then also as the main folder for configuration so i'm just going to change that and call it what it is i'll call it a config oops uh config.settings so i think that's good and then i need to also change in some of these files here it will throw an error anyway if i try to run my application without changing the name now that the oh i haven't changed the folder name yet so let me change that so call the folder config and now in settings i can see where it is here we go i want to change it here and then i want to change it here and there's one place i don't want to change it which is in the instructions up here so that's fine okay and wsgi even though i won't be using this i still need to change this reference or it's going to complain about it okay everything else is good now i'm also going to make an apps folder here to put all of my django apps inside of my django project just to organize them a little bit better so i have an apps folder and i should be good to go so my uh the steps are going to be a little bit different um by the way i already have mongodb running on my computer so it's already running on the standard port 27017 so i don't have to worry about that yeah make sure mongo's running and then i already started my project oh yes i do need a virtual environment this is why i have a checklist so don't forget so let me create a virtual environment for the project um this is just built into python now it's been out for a while so you probably know but python dash m vm and then i'm gonna call the virtual environment vm as well okay so that should be creating my virtual environment and then of course i go into my virtual environment first thing before i accidentally do anything outside of it okay now i'm inside my virtual environment and i need to start poetry so this will well you'll see in a second so this is if you use javascript this is kind of like npm in it except it creates a python specific file where i add my dependencies and so this walks me through the steps here what i want to call it version number description author is fine uh license mit compatible python versions um yeah 1.6 or greater is fine would you like to define your main dependencies interactively uh no you can i could type in django and all that stuff here but i would rather install them myself okay good so here is my uh poetry file you can see i'm blocking it a little bit here uh i can move myself over here there we go so [Music] yeah so there's my poetry file on on the screen here and you can see in the code okay and you can see in the code here there's my virtual environment um but it should have oh i haven't confirmed it yet that's why so let me confirm this uh confirm generation yes so now it's there okay so now i have this pi project.tamil file and it doesn't have any dependencies or dev dependencies i guess the only one is python itself right now so i need to add some dependencies first and so i need to install so i need to do poetry poetry ad yes that is the command so i need to do poetry add and then i guess the first one is um django rest framework and i don't need that second library there because that is for postgres but i'm going to install django rest framework with poetry and while that is installing i know i need to install this library jongo for mongodb so it's saying to use pip but instead i'm using poetry and i will just install it the same way once jingle rest framework has finishes and finishes anyway so it's kind of a quiet night how are you guys doing all right i've been so busy for work that's why i haven't been able to stream quite a regular times but i'm just trying to mostly just stream as often as i can because anyway the streams are up on twitch for two months and i also edit them down a little bit and upload them to youtube okay so poetry add jungle okay so those two very important things are being installed and let me go through my steps by the way if you're joining me and you want to know what steps i'm going through i'm made this wiki which i'm still making trying to make better and better but these are basically the steps that i take to set up django projects um okay so i already did this oh i do need this init.pi file might as well go ahead and add that because i created this directory so it needs to have an init.pi file in it if i want to traverse my directories as packages so let me add that init.pi of course that's an empty file that i just leave there um okay so you can see that the two dependencies i installed were added here so i'm liking poetry i've been using it for a little while now but um it's definitely it definitely has a lot of extra features switching over from pip to poetry no more pip freezing and i also have this poetry dot lock file for my dependencies and peer dependencies and things like that okay so i have my main dependencies installed here okay so let me go ahead and set up the application so as i mentioned i'm not going to create users for this app at all because i'm just doing a quick setup for mongodb mostly okay so i do need to set up the database but i can't follow these instructions because this is just for postgres so i'm going to let's see here's the getting started for django um those are that's the documentation so this is the library that i'm using to interface with and in my settings my main settings file basically i just need to define the database engine which is jongo and then name which is the name of my database and that's it so uh let me just copy this part actually and in my settings.pi i already have the database section here and i can just paste this in okay so i have john go my database name will be let's see spaced learning because it's a spaced repetition learning back end okay so i think that should work that's really the only setup step that it has in these documents in this documentation requirements if your models are nested queries or sub queries that values name okay so it's basically showing how to use the regular django orm now for mongodb i'm pretty sure i have mongodb 3.6 or higher hopefully i do otherwise i might have to update it okay so let me try running this application i'm in my virtual environment install my packages okay so it should run let me try python and also with postgres you have to create the database first but with it creates it for you if it doesn't already exist so i'm guessing it should just work right now let's see run server okay so improperly configured jango isn't an available backend alright so it says my sequel oracle postgres or sqlite [Music] um no uh module named data classes okay so that's with the same error let's see the above exception was the direct cause of the following exception which is okay connections default database it's yeah this is kind of hard to read details load back end and properly configured okay so i'm gonna see if uh this oops let me just highlight it and it copies so improperly configured django isn't available database on the back end i'm guessing other people have had uh this same issue so let's see support okay yeah let me go to the github and see if i can quickly find an issue not working for django version greater than three oh not compatible with django 3.0 well this is an old um an old issue that's over a year old django isn't available database backend this is like almost two and a half years old okay so get this i'm gonna see if they have in case you didn't know okay so they're saying i might have to install uh bson which is okay using django 2.2.10 and uninstall vsan and remove the directory okay but that doesn't really fix it doesn't really fix not working with django three i searched i searched through this repo and it seemed like is there any support for django 3.1 so maybe if i downgrade to django um just 3.0 it will work less than or equal to 3.0.5 um oh let me just go back in the browser and see i'm guessing uh nobody has any idea about this um mongodb isn't that popular with django or i guess not not that's not that popular but it's not that common commonly used i think okay uh django isn't an available database back end this is from two and a half years ago so 48 comments um you haven't installed junga what okay both commands don't work for me i solved the problem by removing virtual environment and reinstalling all of the packages again okay but this guy's randomly talking about my sequel activate virtual environment source vm bin activate deactivate simply deactivate your application and artifacts by recursively removing it okay yeah people just saying to install jungle it's already installed okay doesn't make any sense okay i do need to install i guess in my tamil file i don't even have django here so i do want to put the django version here so let me try to install um poetry add django um let's see okay cool solve problem error this is what's another nice thing about poetry because jongo 1.3.3 depends on django between 2.1 and 3.0.5 okay then that's what i want to install this version so that's the same thing the person said in the comment so now i'm going to do poetry add django equals 3.0.5 and did it work yes okay cool so it installed that right that correct version so oops so let me see if it works now because i think nope still doesn't work but it says it's compatible with 3.0.5 there must be something else going on there you scroll all the way to the end so this person has the databases oh he's using sql light but then for analytics um i wonder if i do need the host and everything do i need that is name engine and then client let me try adding that because i already have my mongodb running so [Music] let me add that here oh no it would be in here right um okay let's see so i have a username password all source yeah get rid of those so anyway i have the host this is the default host so it's probably going to do this anyway but i'll just go ahead and add it performing no module named jongo yeah that's the same thing that i'm getting okay so after lots of search this was my last bug stop upgrading django 2.2.8 to django 3.1.5 and it worked um okay we fixed it switching to python 3.6.8 okay well i mean i could do that if pi ends i could just install another python version but um let me try one thing at a time here so um this is always good running into these problems i guess starting with because then i'll know to put them in my readme so i don't run into them again um okay so django 1.1.3 what version do i have let me take a look so in my pi project yeah i have 1.3.3 and then django [Music] 3.1.5 so i don't think actually python 3.6 here um i could change this to 3.6.9 um it seems like that would be because it won't let me install 3.1.5 for django so it seems like the change i should make is upgrade is upgrading python and if i look at my python version now uh python version 3.6.5 so let me do pi end let's see i am shell i have 3.7.9 let me try that so now let me do oh oh because i'm in the virtual environment let me get out of here let me remove my virtual environment so rmrf vn and i'll start over okay so python shell three points no not it's a pi end so i'm changing my um version my python version to three point three point seven point nine and now if i do python version 3.7.9 okay um so that should work so now i'm going to do python-m start a new virtual environment oh man so the last the last stream i did i tried out ant design for the first time that was not working with v3 so i ended up having to switch and now uh and now i'm trying to use my for the first time with django it's yeah it's interesting okay so now let me get into this virtual environment and oh hi c films how are you oh i'm doing i'm doing pretty well yeah i was yeah i was just trying to figure out how to get to work with django which is something i've been wanting to do for a while environment yes this is the environment that i want and whenever i use something for the first time there's always a bunch of issues that i run into i always hope that it goes smoothly and it doesn't always just go smoothly but yeah it is a good weekend especially because we have tomorrow off um i do relational yes relational seemed to be a lot easier my sequel i haven't done my much my sequel i usually use postgres um it just seems to be what companies around me use and it's also what django recommends that you use if you use django hi eng 2802 thanks for the follow um okay so django srl oh yeah so that's what i need to do in here it's saying python at least version 3.6 but i'm going to change this now to 3.7.9 because i made a new virtual environment for this so i'm going to try and see if that helped anything django django was a movie um people keep telling me it's a really good movie so i want to see it but i have not seen it yet but whenever i search django it comes up with that movie django unchained so i have to do like python django or something um oh yeah python let me try running my app again oh couldn't import django oh i haven't installed that's right poetry install because i deleted my virtual environment where all my packages were installed so now i have to reinstall them so they're all installing right now oh pie i oh i guess um jungo relies on pie but i might have to just use pie straight if i can't get jungle working you do web development and databases nice where are you from just curious i'm always curious what country someone's from um okay django rest framework jungles installing um i do yeah i would say most the majority of my work is web development and then i also do some teaching now at a coding school so i do i guess i do work in django and different javascript frameworks and that's been really almost all of my work for the last year has been doing those things and even before that i was working with a lot of python and javascript i may or may not be in the place my username is short for c films i don't know what that means um maybe it's just late hi javavar how's everything going in argentina um okay wow it is late okay let's see python rest framework okay so let me try running it again now that i have everything installed hopefully it just runs now oh seafilms.com is your website oh okay i'll have to check it out oh please i think it's funny uh i hope it's running oh i don't click on links during the stream but i will look at it after why isn't it shouldn't it be running on a ah it's not running okay it says connection refused why would it be refused mongodb connection refused so i do have running though let me see oh thanks for the follow c films um let's see what do i do okay so i'm on a mac let me clear this i'm not typing at the bottom i'm on a mac and i do brew services i think it's list yeah you should list everything it's running kind of slow okay try to put them on one line there we go so i have elasticsearch okay yeah i have elasticsearch postgres redis i have all those things running no wonder my computer's running high okay and then i have mongodb community um which is good so um my mom go here refresh so this is running five dbs five collections this is running on port two seven or it's yeah on port two seven zero one seven which is where mongo's running so it's getting all the databases from and well at least it's a different error now it seems to be now that my upgraded my python version it's working um okay let me try running it again otherwise i'm gonna have to debug what's wrong with credentials error it could be yes if i need to have an admin user with the thing is like i know how to create one in postgres but i can't remember how to do it in let's see what's this collection oh that was a demo that i did before okay so how do i get back to the elena's rob admin config and then all right i don't want that okay so i need my database i guess i could create a database here and maybe it's just not creating the new one which is weird many american children mac are you saying many a lot of americans have macs um well a lot of companies i work with use max so it's just yeah all the i don't know why all kinds of developers use mac um let's see database name what did i call this uh space learning sounds like a sci-fi thing custom collections invalid name spaced learning why would that be invalid i guess i need a collection name so i will have a cards collection so let me add that okay here i have space learning i have cards i'm going to create another collection called decks index cards will be part of decks okay so i have these two now let me try one more time uh to get to work otherwise i am going to try with try adding a user to it and see if i need to um add a user most companies around me use pc interesting the only companies here that or the developers use pcs um are pretty much like microsoft or companies that use microsoft like.net c sharp that kind of thing why are using instead of postcrest that's a good question just because i wanted to try manga with django um yeah generally i use postgres so here's the same thing i'm getting the yeah the connection refused error from let me see these aren't real easy to read time out uh yeah so it's trying to connect that's why it's taking so long here because it's trying to connect and it's not so uh how are django and mango related um so in the settings and this is really the only setting they said i had to set up here was jongo and and the name of the database and that's it so let me actually try that and see if that works have i tried flask oh invalid syntax oops okay um yes i used flask for a long time before i started using django oh whatever it works okay so the issue was my python version when i upgraded my python version it started working but unfortunately i had already added a line of code that i didn't know was breaking it so yep appears to be working now which is good i can go to well i need to run my migrations but i'll go to localhost 8000 and this is the django landing page of course i'm going to set it up with django rest frameworks no absolutely not companies most of the companies here that do use python are using it for data science there are a few django companies though and i am fortunate to work for one of them do i do c plus not really i did a long time ago um okay so yeah let me i need to set up and also the rest api for this so what to do first let me set up rest api because i need to create a couple basic endpoints and then i'll need the models i guess it doesn't matter what order i do this in but i'm just going to set up some end points really quickly um let's see add drf oh yeah i need to add some of these things to installed apps and it's weird because i don't didn't have to add django to installed apps at all so or i guess i didn't anyway okay and i haven't made any custom apps yet either so i'm just going to add rest framework here and then i need to create the urls.pi files and then set up my urls so first things first i'm gonna do python manage.pi uh migrate and that will run all of the like the admin all of those built-in migrations for django okay run all migrations oh something this version of jungle does not support null for [Music] validity okay well it seems like everything else was okay and there wasn't an error that stopped it um can i refresh this uh here's space oh sweet look at what it did it added all of these tables yeah so i guess i don't need the cards and decks i forgot uh jenga is going to create these for me sweet drf is django rest framework um mostly php oh cool i haven't used php in a while but people say it's gotten a lot better with new versions version 8 or whatever version it's on right now is it difficult to do in e-commerce it depends on what you're doing i'd say in general it's a more it's a more difficult app because there's a lot of different features to implement um but there's also a lot of people who've done that kind of site before so you can learn from a lot of people who've gone there before okay um framework oh yeah i need to in include their urls in my main urls file so this is for django rest framework oh yeah inconfig urls and add that um oh yeah i need to include okay so i imported include that's good now in my apps this is the next thing i'm going to create two apps no users this time but i'm going to create a make directory let's see apps slash cards and apps slash dex so the apps or the cards and decks i need to make both of those apps and now let me do python manage dot pi start app and then my first i'll create the cards app and i'll create it in the app apps slash cards folder and then i'll do the same thing for dex so i'll create the dex app and then oh hi cm griffin how are you thanks for the raid it's good you missed all of my terrible struggles with using it with python and now i've finally got it working so welcome everyone how are you guys what were you working on tonight all right thanks for the follow alex heart okay so i created my first two [Music] django apps here cards and decks and i'm going to build off of those right now by the way if you want to know what i'm doing i'm building a space repetition back end i've already built two back ends for this site one in graphql one with postgres and the rest framework and now i'm using the rest framework and um oh yeah that's the theme of the stream manga so that's what i'm trying to do right now okay so i've created my two apps now i need to add models and url files to these and i'm gonna get started with cards oh you were doing c cool i i did some c streams and i struggled really hard it's not easy especially if you don't work in c every day let's see um cards oh yeah urls i need a urls.pi file this will be quick i'll just go ahead and add those so let me get a starter from django arrest framework yeah i'll just look through the docs all right almost 9 p.m where i live but i can stay on a bit longer let's see api guide um where would it be urls views routers here we go i just need like a real basic router uh drf i need a like a auto response for that question django rest framework yes thanks cm griffin um okay so i'm gonna use the simple router uh there are quite a few other router options for django rest framework but all i need is a route here my main goal is to get working so i'm going to use whatever here okay so let's see import router router simple router so i'm going to get rid of this and then yeah i guess i'll do it like this and then the only thing i want to do here is yeah i'll leave this empty actually because i'm going to point to this urls.pi file from my main urls file here and i'm going gonna say let's see restframework.urls no no i want to do dot apps.cards.url i'll just go ahead and put the decks in here too dex because i'm going to do that in a second okay and this will be at the path um yeah cards so that will be at the cards path and then so i'm leaving this as just the default because it'll just be cards slash and then it's going to automatically create item endpoints as well so card slash id all right uh okay so now i have urls i have no views so let me create a view of some kind here uh let me do yeah cards view set i'm just gonna have one v set in here so okay so now i have cards reset oh yeah i have to import that so from views in the same directory i'm going to say import cart oh cards view set that's fine and now i need to create the v set here and also create a model here okay so let me look up and get a basic view set too here's views generic views view sets view sets are nice because i can just add not that well for a model view set i can just add the serializer class and the query set and i'm good to go so let me add that um what was i doing it was cards view set okay serializer class and then i need to do cards i guess card serializer and then i want i need to create the card model but that it'll be called card and then i need to uh from models import that card model and i also need a serializer class i'm going to put the serializer in the same file because i only have one so let's see class card serializer this will um serializers everything seems to look like it's spelled wrong today okay so oh i need to do serializers let me import serializers from uh django rest framework so jing no from rest framework import uh serializers let me see if that works let me look up serializers coding streams are kind of heavy right like it's hard to i guess follow along fully um yeah by the way here's a wiki i've been setting up to kind of as a starter or no a guide to set up jingle projects i have different kind of jingle projects on here uh graphql mongodb like how to set up postgres and users and different things so i'm building this out as i do every stream so if you have any recommendations or things i should add to it then definitely let me know okay so wait a second i think someone raised issues here fix source oh cool awesome i'll have to take a look at those oh sorry it's been a month sorry mikey stan i didn't even see those okay anyway uh serializers do you use any special package for user authentication in drf yes i use a jwt library i think it's like rest framework jwt or something so rest framework does have a built-in jwc library for anyone who doesn't know but i i don't know there were some things that was missing i think uh expiration blacklisting all of those things like it doesn't have that many features so if you're going to use this in a real application or professional or whatever application that you're going to deploy you probably want to pull in a library for that um by the way thanks for the follow ferven and legendary urban hacker thank you okay so let's see the comment serializer yeah i don't need all of that um from restaurant yeah from rest framework import a serializers i just wonder why it wasn't auto completing but i guess that's fine then i have my serializer that i'm gonna hurt from serializer yeah serializers.serializer um but i think i can do let me see there's model serializer right yeah and then here i can just set the fields and the model and stuff so i'm going to do that actually set a metaclass so this would be model and then this and thanks for the follow a time frog um yeah oh yeah thanks a litty um okay do people still use django yes lots of people lots of companies especially startups still use django we we build new django projects all the time for clients at my company so well at one company that i work for um yeah so it's still very widely used so here i'm going to replace these fields i wonder if i can just do all like just pass back all fields and then the model will be card okay so now i need to go create that model oh wait get rid of this irrelevant comment okay so now in the models here let's see i need to create a cards model and this will be class oops good okay so this will be class card that will extend from models.model and let's see what fields i mean so a card it's like a flashcard so i need the question i need an answer i could have like created that updated at those kind of things but just kind of keeping it simple here so i think i'm just going to do question and answer for right now oh all without a list okay yeah that makes sense okay let me try it and see um okay so here i'm just gonna say that these are i guess it should be a char field right models dot char field and then do i have to set the max length here max length equals total 55 and then answer uh dot let me put this as a text field and then there's no max length for that okay so here's a card and now that i have a card i have my model i have my url um i should also be able to register this real quick in the admin interface just to complete everything so um i can't even remember so what is it admin.register or something let's see django admin admin.site.register thank you okay so admin i won't do a custom admin class i'm just going to use the default so admin.site.register um and then that will be card so i need to pull that in so from models import card and then there's one thing i'm missing hey dex final how are you um one thing oh yeah i want to do a default here so i'm going to set the string so that way it's not just um like object so oh wait it's def let me put a function here def string and then i think that's how i do it so i return wait here i get self right self yeah so i'm setting this default string as i guess question uh or self dot question okay just started learning python and django cool welcome um yes django is awesome for building applications quickly let's see return self.question okay so i think this is all working uh i can try running it but let me try to make that migration first to create that table for cards so python manage.pi migrate no make migration make migrations nope oh card i need to put it in apps isn't in installed apps yes okay so i need to install that i've added created modified data in all my models and fields to appear in django admin but they are not pre-populated i'm not sure what you mean by that okay so i need to add it here yeah so i'll do apps dot um cards yeah right is that what i do let's see in my apps yeah name cards okay so i think that's good just cards i'm inside of apps though i think i have to do apps right yes i do i have apps here so it's inside of apps and then cards because i didn't put it in my root the default is to put it in root but i don't like you know putting a whole bunch of folders just in my root directory okay oh you said created a modified data to my models and the fields appear in django but they're not pre-populated oh you have to use that are you using auto now add or whatever it's called uh like auto ad now it's something like that there's auto ad and auto ad now okay yeah thank you um those should work okay so common apps dot cards okay let's see if this works now if i do make migrations again view sets is not defined of course so in views serializers from rest see it keeps trying to import this jwt my ai is not working right now um let's see let me go to view sets okay from rest framework import view sets okay rest framework import view sets and let's see if this works this time okay cool got rid of my errors it created the card model um yeah that's all good and now if i go into let me refresh here and go to space learning and session where is it it should be under cards right uh yeah it's the cards app so i think it would be cards card but i already created this so this shouldn't be here this is something i created out of my own ignorance i'm going to delete it drop collection cards job collection okay and here too let me drop collection decks so why didn't it create that for me this is weird space learning oh what am i doing i didn't migrate thanks yeah i just made the migration so i didn't actually migrate let's see uh yeah it created the migration for me but no migrating okay yeah so let me actually migrate this time python manage that pi migrate version of django keeps giving me this warning null not null column validation check fully okay um i guess that is something to do with using no sequel so i'm not too worried cards card here we go sweet awesome okay so now i have cards card i want to do let's see oh that feels so good to set this up i'm glad i got it working okay so now let me run the server and see if i can interact with it and add some things to the database so python manage.pi uh run server i wonder how it handles foreign keys i guess it does actually even with ids and stuff generates an id automatically well i guess so does postgres so it's just different kinds of ids okay so now my server is running let me go to oh wait here my server is already here and this should yeah because it's no longer the default django page now that i have the rest framework urls there thanks for the fallout sandbox um okay so i have admin cards login log out um yeah so let me go to cards and see what's going on okay so i can create a new card i should be able to make a post request here okay let me try posting yay it posted and now because i'm using the rust framework router i can do card slash one and sweet and then i get that card back in my get request and if i do just cards i get all of the cards that i've created um cool and then i should be able to also see these in the admin interface localhost 8000 slash admin oh i didn't create a super user okay let me stop this real quick stop my server oops clear clear is probably the most common command i type in the terminal um okay so this is python dot pi create super user um okay and since i didn't yeah i didn't update my user at all so it's just using the default django way with the username which is fine i'm going at example.com and the password okay super user created successfully so let me run this again and sweet so now i can log in with my username and my password and i have cards and i can see my card here cool so i think that's all um real quick i am going to try to make the decks app let me close all these windows for first okay let me try to make the dex app real quick and oh yeah i forgot i already started the app and i'm gonna see about the foreign key relation actually first i'm gonna uh where's the refresh here open in tab oh yeah refresh cool so yeah i see it in here oh this is weird though it has the underscore id but then it has another id that's the auto incrementing can you see it oh cool um yeah it's the auto incrementing id of one but then it also has the object id that creates um i guess maybe i have to set the id somewhere to tell postgres not to add this id for me i don't know okay um yeah so i'm gonna copy some things over from cards here just to speed things up like the urls is pretty much the same except it's going to be a dex view set text reset okay and that's fine and then in views i'm going to go to cards views get all of these and import deck that's going to be my model here this is going to be deck and almost exactly the same serializer class deck okay so that's all done i need to add a model and actually i think the card is the thing that should have the foreign key to the deck um yeah because there will be a bunch of cards per deck and cards cannot be associated with many decks it will just be one deck so um okay so models let me to copy over the admin too why not okay and then the admin here so i'll change this to deck and then this to deck okay so get rid of all of those and now i just need to create a model for decks here and this will be class deck this oh yeah extend model stop model and then i'm just going to give the deck a name real quick so this will be a char oh model is not charcoal that's right models dot char field max length equals 100 and then i will do the you know i need one of these on every model um okay that was convenient so yeah so i have dex that only has a name field and that is fine for now and now i'm going to try in my not here where is it in my models here i'm going to have a foreign key here so i'm going to have deck a deck field and this will be foreign key to the dex model by the way thanks for the follow hacking sports oh hacking esports welcome okay so models dot foreign key and this will be [Music] deck and then i need to set on delete and that will be models so basically when the deck is deleted is the card deleted um sure why not cascade okay that works uh oh yeah i have to import decks then so from apps dot uh yeah apps.models import deck okay now i have to run make migrations because this migration has to be updated and i have to create the decks table as well so related name cards can help in the future as well i think i can already access this by cards because it goes both ways right um i basically the only time i've ever needed related name and you can correct me if i'm wrong here but it's like when i'm referring to the same uh the same model twice here like if i had deck and i don't know a special deck and they were referring to the same model here then i need to reference them in different ways but i don't know is there any other reason to use related name there i think you need it to do no i i can get to deck dot cards i can get from decks to cards already because i can go um both ways without that i guess oh i need to add it to installed apps oh and the urls too okay uh add it to install apps let's see dex oops and then urls oh i just need to uncomment sweet okay so this should cannot import named dex vset from axa app stop desktop views okay oh yeah i didn't change it dex reset okay now my server's running so let me try to do the migrations pi uh make migrations a little uh provide a one-off default oh yeah for that um okay i will provide a default i mean i only have one item in the database so i could just delete it uh what would be the default well it's supposed to be a foreign key so i don't know i guess i'll do that um okay if i have any trouble then i'll just delete that one item that's in the database okay so i made the migrations and now i need to migrate manage.pi migrate uh nope so [Music] fault not null okay i think it's throwing an error i didn't read it all but i think it's throwing an error because this i'm just going to try to delete this and c nope okay so first of all apply migrations okay not implemented alter for sql alter table cards card column id and default not no okay so i have a migration that i made this one's for dex so it's basically just adding yeah primary key true and all that stuff i'll have to change that but that's fine for now serializer serialize equals false for both id um let's see and then it adds the name and then my migration that for card name deck foreign key to dex preserve default oh default one okay all right [Music] so so it did create the decks i think it just had a problem with the cards one so python uh migrate cards oh wait what am i doing python manage.pi manage.pi migrate and then if i just want to migrate the cards app let me see nope okay i'm still getting a database error [Music] from okay what i'm going to do real quick just to make this quick i'm going to delete this since this migration hasn't been applied anyway so i'm going to delete that and then um in my models so for cards here i'm gonna say that i'm gonna clean this up a little bit let's see i'm going to say oh come on uh null equals true um and then do i have to do blank equals true on a foreign key why does it keep trying to put stuff in here blank equals true i think that'll fix what it's complaining about but now i have to do that make migrations again holy coaster blanket goes through cool thank you um manage.pi uh what is it make yeah make migrations okay cool so it created that card deck again on delete yeah and i deleted the thing from the database so i don't have to set a default this time so i can just do migrate oh cool oh not cool i thought it was going to run wow it's not happy uh okay not implemented alter command for sql alter table cards card and column deck id and no alter table why can't i run an alter table command cards on the on the table cards card i'm trying to add the column that guy d can is it because i can't do with null equals true you don't need a default anyway yeah that's right um hi hacking esports thanks for the raid how are you um yes so you can see i'm trying to debug this error right now on my project um by the way i am doing django so i'm trying to set up a django application similar to this one i've already set up but that one uses postgres and the one i'm setting up now is using mongodb and i have it partially working but now i'm trying to set up a foreign key relationship in and it's not working thanks for the follow everybody yeah so mongodb and post in not in postgres in django it's it's uh fun okay so i don't know why it's setting the migration dependency as oh i guess i need yeah i do need decks because this is setting up a foreign key with dax so i need that uh collection to exist thanks carlos thanks everybody all right django similar to flask no they both use python but no flask is like a microservice framework i mean it doesn't have to be for microservices but it's a micro framework django is like everything batteries everything you ever imagined um let's see okay so i have my uh foreign key this should work is the thing i think it's just uh like django the library i'm using for um remember that error was giving me earlier that it didn't says it couldn't do nulls or something i'm wondering if that's what it's complaining about right now so that foreign key can't be null yeah i'm wondering let me try since i didn't apply this yet i'm going to try to delete this migration again and see if let me just take these out i guess ah it's late it's trying to do that oops okay and now i'm gonna try one more time to do make migrations i'm gonna well it's only cards anyway that i have to make a migration for provide a one-off default there's no no there's nothing in there already uh no i don't want to provide a default all right to the machine thanks yes i might try that okay so uh let's see python so i could roll back what i could do is roll back all the migrations and cards here um because the dex migration doesn't depend on cards it doesn't need cards so um i might do that why do i always get stuck in migrations at some point even a simple app complex app any app okay um not run but let's do migrate and then the name of the app is the cards app and then i think i can do zero right was it zero or was it capital zero rendering on applying okay so unapplied all the migrations so far okay so now it unapplied all the migrations and yeah and there's also nothing in the table that i no data that i have to worry about yeah there's not even a table there good so now make migrations now this will just make oh no no no no no stop stop stop stop uh okay delete let me delete the current migration that's there because i just unapplied them all oh make sure no migrations okay good one more time let me make migrations um yeah i have no idea what's going on in the chat yes thank you okay yeah i guess i can't use null fields for foreign keys in uh manga wait where's my cards oh oh oh never mind nevermind this is just make migrations i need to migrate i keep forgetting uh my great cards okay please yes okay now my table should be there does the foreign key point to a primary key um that's a good thing to point out because oh yeah it does it does because it's oh i guess you can't see here yet but the id field and not the id field but it's creating a custom id field just by default because i haven't changed that so it is pointing to that id field so it is pointing to a primary key okay let me try running this see if it runs oh yeah wrong server okay and i should be able to i have no cards in here i also have my api here so i can do deck oh yes i'm going to have to create a deck first so let me go to the decks endpoint and dex detail not found i guess because there was nothing to get um yeah i wonder if this api even works let me call this test deck um this should be a post not a put yeah something's wrong with dex let me try to debug what i did with dex here and then uh let's see dex hairstex v set deck uh dex serializer deck.objects to all all of this is right um urls i have dex view set and this is pointing from here i have apps.urls oh that's right okay i don't have my um url properly set up okay so now this should work yeah now i'm not getting that weird error so here on name i can do it's getting late i'm tired i can do test deck now it shows me a post okay now i have a deck so now if i go to the cards end point here i can do test question test test and then it go i can point it or link it to test deck i'm glad fast api is a thing now jingle and flask feel old i haven't tried fast api maybe i'll try that okay sweet so i created a card i can create i don't know let me do two and two okay so now i've created two cards i can get them both back unfortunately i don't have an end point to say get the cards back filter them by deck but i don't know maybe if i do another stream i can do that okay and this should show decks yeah and i can see my deck here okay everything you know all the basics of a django app are working now uh let me push this to the repo i made so i made this django srl srl is short for spaced repetition learning so let me grab this url here and well first i need to get in it and get remote add origin post this and actually one more thing i need to do that i don't think i have here yeah i need a git ignore file so let me just add that real quick before i'm strange things um yeah let me go to get ignore dot io and if you get the decks is it showing the cards as well no none of that is working right now all right python what else do i need to ignore vs code i'm on mac but i'll just put windows there for whatever of course vim that's important um uh let's see linux i think that's it yeah let me create that okay so that should be let me make sure because last time i feel like it my pie cash wasn't in there pie cash okay maybe i forgot to do python for some reason okay my virtual environment it's grayed out now that's a good sign so now i can do git status i need to add everything apps config manage lock yeah i'll just add everything oh yeah git commit dash m first commit and get push dash u origin master have you tried using python yes i actually have the i've had pi term community for a while and my company got me pycharm uh the regular or the full version so i've been playing around with that too um yeah i like it it does a lot of things for me but somehow i kind of like a lot of the things that it does for me aren't really in my way i guess like setting up virtual environments and all of that stuff for me um i need to get used to some of the more advanced features of pycharm and then i think i'm going to have a reason to want to use it more basically why go backwards to pycharm pycharm does a lot of things though and from what i've heard it runs faster than vs code like if i have a whole bunch of things open vs code isn't bad though and i think it's even improved over the years since it launched but i just heard that pycharm was more efficient although i have not i have not tested that okay so here is the repo where it should have pushed to access that yes databases that's what people people have been telling me that um uh yeah basically someone was explaining to me how they use the database client from pycharm which is really nice so this is the repo first based repetition learning if you want to take a look at that's the manga one that i worked on today there's also uh you code react in pycharm oh i didn't even think about doing front end and pycharm there's also that one which is in graphql and this one which is the same app but just postgres and django rest framework no graphql and no so i thought it was cool to like build the same thing in a bunch of different technologies and kind of compare them so yeah i hope you liked it and here's the wiki also i'll go ahead and post this in the chat that i've been working on so if you are interested in giving me some recommendations for how i can improve this wiki for setting up jingle projects then let me know yeah so i hope you enjoyed the stream tonight so sorry so let me just do a very brief intro since i wasn't on the mic a second ago so basically i am building off of this repo which is uh mongodb with django i just got the basic setup pretty much last time and last time i set up these two endpoints basically with slash cards and flash cards slash id this is using django rest framework and then mongodb as the database the endpoint i really need to set up is dex slash id cards so instead of pulling all the cards back i can use this endpoint and get the cards that are just associated with a single deck and so i'm going to have to figure out how to use the django orm to do queries so that's going to be fun or maybe just queries with jongo itself instead of using the django rm and then something i've set up before but uh what might be fun to set up with if i have time at the end of the stream then i will set up some filtering maybe searching sorting that kind of thing so um django rest framework has some really good options for that kind of stuff so that's always fun to do so yeah let me start with trying to get this endpoint set up and let me see so here's my code let me close that so it's um basically i have the cards app and the dex app and then i have my config with you know all the standard django config settings urls all of that stuff so let me run my server i'm already in my folder and inside of my virtual environment here so i'm just going to do python manage.pi run server and it's on port 8000 so if you do localhost 8000 it should come up with the django rest framework page that and this page shows me all of my endpoints that i have access to right now so let me just do i'm not doing users in sap just to simplify it so i can test out without having to worry about auth stuff so i have uh ability to do crud operations here and i also get back all of my cards now these are all associated with deck one um and i want to try filtering so in order to or i want to try pulling from a single deck and now there's only one deck so i need two decks to make sure i'm doing it right so i'm first thing i'm going to have to do is create a second deck i hi juhurt how are you no sorry i will get a new laptop at some point but this laptop is pretty old and runs really hot so i could try to turn my mic maybe and see but yeah i keep saying that i will and i will pretty soon but just give me a few months and i'll have a new setup with no background white noise so sorry if that's annoying um okay so admin cards decks login logout okay cool um oh yeah let me go to the admin slash admin and i can see cards and decks let me create a new deck from here it's just easier than in the normal rest framework place and i'm going to call this javascript save so javascript testic i'll add a third deck i'll call this python save that hi yamamura thanks for the follow all right cool um there's three decks now so oh yeah let me go to cards and associate some cards so i have these two test questions they're associated with the test question deck so i'm gonna just make some dummy questions like what is python and associate this with the python deck and then i'll make one for javascript so let me add a new card uh what is js and then javascript okay so i have some different cards with different decks this is a good setup for filtering and okay my server is running here's my code so now the next thing i need to do so index i'm going to put my url index because i already have this url which is basically the slash dex url and so what i need to do here is register uh slash dex slash oops slash id slash cards url here and this has to be a nested router thanks for the follow i'm in break how are you okay so i need to register this url here and to do that i actually need to import or i need a new library so instead of rest framework i need to install the rest framework nested to kind of easily set up those nested views it's much easier than doing it manually so uh let me see where routers um so if i go i'm in the django rest framework docs and goes over all the routers but then if i scroll to the bottom it kind of has the third party packages and drf nested routers is so useful so i can make nested routes and give some examples slash domain and then this and then slash name servers and it gives you multiple uh nested i can't even remember the name so there's um not methods but thanks for the follower java um yeah it'll come to me raw resources nested resources and item and points that's what it does and installation i can just install it with drf nested routers now i am using poetry here so i'm not going to do a pip install and it will add it to this toml file automatically so i'm going to stop my server and say what am i doing poetry ad and then the package name and that's installing so now i should see it yeah here drf nested routers and the version number okay so now i can import this in my project and quick start how to do it yeah basically i'm going to instead of importing routers from rest framework i'm just going to import it from rest framework nested and that's going to give me a lot more routing options so i'm going to copy this and just basically replace that line and here we go so i'm already getting my views and this my default router is going to change okay that's already simple rather good um register yeah this is the same thing that it would be otherwise and now i need to create these this nested wrap so i'm just going to copy this and update it so i don't have to hand type out any everything and basically i have to give a name to this router and um oh wait i'm going to call it let's see i guess uh card cards router yeah because i'm already in the dex urls file so everything is associated with the deck in this file um let's see i pass in the router so that's correct um and then this this uh string here has to match the url string from here but it's just an empty string because i'm defining what the route is in my config slash urls file so you can see my apps decks urls file already has dex slash in it so i don't need any other [Music] additions to that endpoint so i'm just going to leave it like that um look up let's see dex um okay so cards router dot register now here's where i register uh nested my nested route and so i'm going to call this cards and this route because i'm registering it on top of this router so it'll be nested which is what i want so it'll be dex slash id slash cards if that makes sense thanks for the follow up piece within is that what it's short for welcome to the stream uh so name server view set i need to create a class for this so i'm going to say deck cards view set yeah um base name i think i don't need a base name if i have a regular view set but if there was an error i can change anything okay so url patterns now that i have more than just the plane router i do need to change this so i should be able to register urls here if i remember i think it's a list of tuples and then i can say let me see views yeah url well this will be path so i wrap it in path and then i need an include two so let me do this path and then include and then here it's router.urls thanks for the follow joseonks how are you oh and rodrigo oh it's good to meet you too rojica um do either of you do any django let's see include oh how's everything going in croatia um i haven't actually been to croatia but i've kind of been around it so hopefully one day i will make it out that way um router oh yeah this has to be the second router so i have to register both routers so cards underscore router okay so now i have my router's registered but it's still going to throw an error because i don't have this class here so let me just clean this up import the correct classes and now i need to create this deck cards reset what am i working on so i'm working on let's see here it is i'm working on this repo so i did this repo i already did the setup for this repo last sunday and now i'm basically trying to actually get a useful query in here um that'll pull back the cards associated with a certain deck it's called this repo is called django mongol srl srl stands for spaced repetition learning so i've built several of these apps you can kind of read in the repo here i built two different versions of this same back end for a space repetition learning system so you can check those out if you want and i'm using mongodb with this jungle library yeah and let's see so i have to create deck cards view set so here's my views very very simple views file so i need to create a class and extend from view sets dot let me try model v set okay and i need a serializer class here and i think i can just actually borrow the card serializer because that's going to give me all the fields that i need i can just import that from this other folder so uh thanks maya bang uh let's see so if i import the serializer oh yeah i put my serializers in views because it's a small project but i have this card serializer that should give me all just all the card fields back in the format that i want so um you know if you're new to django the serializers take care of serializing and deserializing data so it's in the format that you want when you're getting it from a request or you're sending it back to whoever requested it from the api so okay so card serializer let me import that from here so from let's see apps dot cards dot no uh views import python is so great um card serializer okay let's see so query set will be and then this is where i want to really um filter let's see cards dot objects dot all but what i'm going to try to do is actually overwrite the list method so in django rest framework the list method i can create a method called list and basically if i'm pulling back all cards i can it'll run this method and then i can make it filter to just the cards that i want and i think i get self and request on this oh and i'll also get um let's see i'll get another variable so i'll get oh this should not be oh yeah that should be that yes um let's see i think look up should be dex because look up i think it's lookup that will make it dex underscore i deck underscore id so then here because that will be a route param so i can get deck underscore id because django will append this id to that lookup so it'll be called deck id maybe that'll make more sense when i actually do this um oh through free code camp awesome thank you yeah i'm working on some more stuff for free code camp too that's such a good platform and that's helped me that helped me out so much actually when i was looking for my first job back in 2015 they're just a great group of people working on that um okay so my query set so list let me see if this even works like if i created any bugs yet okay cards is not defined so i'm not importing cards let's see um cards oh that's from view so i need to import the model so let's say from apps dot cards dot models import card and that'll import my card model from over here um which i have question answer and deck okay and deck is what i want to filter based off of so now i have my card imported wait cards oh oh oh oh right i accidentally made this plural so that should fix that error oh path is not defined i have to import those and those are actually from django not from jenga rest framework so i need to import path and um let's see import path oh they don't have it in the example here but i basically i need to get the include and the not url because url is kind of older but i need path and include and let me just look that up real quick where i get that import from so django import path and include django urls um okay from django dot url's import include impact uh perfect okay so i need that let's see yeah i'll do it that's oh this is completely the wrong file let me put it here and oops there we go just like to keep my stuff in a certain order first django avenging a rest framework then packages and then my local imports okay so now my server seems to be running without error that's good um duckduckgo oh hey seeker of truth it must be late where you live thanks i'm glad you tuned in have a good night um yeah go is great you have to get used to it because sometimes you want to make a google query and sometimes duckduckgo is actually better because it's not uh giving you results based off of what they want you to see you know thanks for the follow john cl um so also if you don't know depth echo there's a way like here i can search for something i don't know what that's going to bring up i can search for hello but i can also do bang g and then hello and that will duckduckgo will send me to google so uh duckduckgo has a lot of bang things like bang i can't even remember all the rest like but it'll search github it'll search reddit twitter anything so you can use this bang symbol and then some other character like twitter and then it would search if i had put a search term so i'm gonna get out of twitter though before i start looking at things yeah thanks bang w for wikipedia youtube yeah that deco is great because then you then you can choose you know where you want to search google just kind of keeps you inside of google the whole time um yeah so definitely try it out see if you like it okay so done with that let's see deck cards reset [Music] what else do i need oh yeah i need to now create this list actually i should have that endpoint let me just check if i have that end point now so i have deck decks slash let me do that now i have all my decks let me do slash two now i have that one deck and now let me do slash cards and i have an error okay so list got an unexpected keyword argument dec pk so oh it's supposed to be pk not id that's right okay there we go see django django is helpful okay restarted okay now it's complaining because my list class isn't returning anything but um so this is a valid error so let me fix that now um let me go here and say i'm going to use cards so i'm going to say card dot objects that filter and then i think i can do this um yeah because i'm just using the regular django orm even though i'm plugging it into a database so i'm just going to assume that i can do this as i normally would until something goes wrong and then i can figure out how to fix it the mango way i guess so deck needs to equal deck pk i think that will filter correctly for me and let me set this equal to cards equals i have no other variable called cards okay um uh just in case i was going to use it inside here but i won't be anyway so it doesn't matter and then what do i want to do with cards i need to return a response here so let me return response and then the response i want to return is uh oh i need to serialize this here because i'm doing a custom function for list so it's not automatically going to serialize so i can do self dot serializer class let me see so if i do self dot uh get serializer thank you and then i need to do i think i can just pass in cards now um oh and many many equals true because this is going to be a whole list and not just a single card so let me call this serializer equals greetings from argentina cool how are things going in argentina hi bangor how are you i like the i like the robots um let's see cards serializer yeah so i can return serializer.data that was right because that will be in the correct format to return it in a response basically and then i need to return a status well the status is optional i think it'll return to 200 by default so that's fine i do need to import this response though and i can't remember where to import that from so let me look that up so django rest uh response oh thanks bangor i do like my videos requests and responses it should be somewhere in here return response introduces a response object which is a type of template response okay but it doesn't say where it's coming from oh here rest framework dot response import response okay good um so let me do that and now i can return the response i'm just guessing that this is gonna work but i don't really know so sweet yes it filtered correctly okay good sometimes it's hard to remember like the exact syntax for everything but cool i'm happy about this all right so i have the list and then this is the only method i defined so that list method basically affects um a get request to the resource uh thanks to the follow codec um oh you watched a view video yeah i love i love doing view oh hey ty you know i don't know how to pronounce your name but uh thank you for the raid what were you guys working on oh sorry if my bot is stopping you i might have to fix my bot permissions but yeah welcome to the chat thanks for joining i'm working let me post the repo that i'm working off of so you can see it's a django rest framework with app uh using jongo so hey everyone okay and basically i just got this working so apparently with mongodb i can use the regular django orm which is awesome it's awesome that i can choose to use it with sql or nosql just with a few changes um i can use you know pretty much regular models and queries and stuff like that although i can use you know all those specific queries if i want to yeah mongodb is great for a lot of things oh cheating in academic honesty um uh thanks for the follow rippier that's good because actually it's funny because i just started working at uh a coding bootcamp recently just part time like i work as a developer of my regular work but i started teaching a few hours a day as well yeah so that's a hot topic did you come up with any solutions i would like to know about them all right so let's see card serializer response so i just we just got this list working basically but that is to the resource endpoint um to dex slash id cards i wonder if well it seems like i can post here yeah i should be able to because the serializer and everything is still set up so let me see if i can make a post request oh yeah i just go ahead and make it from down here so i shouldn't be able to pick the deck from here so i'll have to fix that later because if i'm already at decks id cards i would want to just post to that one deck um okay let me see if item end points work let me just do card slash well here's id4 so let me do id4 and sweet i get back that single object so this seems to all work just fine and let me add one more card so how to use js [Music] and this will be javascript deck okay so now i can make another get request yeah it pulls back these two okay so that was quicker than i thought it was going to be so i'm first i'm going to um the next thing well at the beginning of my stream i mentioned i wanted to do filtering if i got done with that other thing i was fixing so i think i am going to set up filtering for cards uh first person shooter okay do you know how to code a first person shooter no um not sure i'll ever be doing that either i've done a few like little mobile games but not not too heavy into the games yeah i like playing games not creating them okay so filtering against current user yeah so what would i want to filter if i were to filter something so let me take a look at what i have here so i have two apps pretty much and two models so cards and decks if i wanted to filter i would probably filter out from the question name maybe um i might be able to sort like alphabetically up or down ascending or descending based off of the question name and then actually if i look in my models here i have the question the answer and the deck okay so i'm going to set up some custom filtering this is kind of a simple app so i won't need to set up too much filtering for it but i want to see if this is any different setting it up with versus postgres what i normally use filter by question type like multiple choice versus open ended um that's a good idea maybe i should add another field to card then let me look at deck deck is just name how sad so name and whatever id uh django gives me okay so yeah let me add another field and get rid of all these that'll make it more interesting so on cards let's see um admin adds models let's say i want to do type type equals and let's do models dot oh i don't want to do just type because this is a python keyword so let me do question type models dot integer field and i should set up choices here and then also a default and then for choices i can't remember what that's called but basically i want to make a question types class i think and then i'll have to inherit something here i can't remember what it is and then i basically want to set up the choices as attributes on this class and let me look up how to do that in django because i forget okay so django let's see let me start with integer choices class maybe that'll come up with the right thing because i don't want just regular choices um okay so if i did a regular like tuple of two bowls or list of lists or whatever um with choices i could have just the integer and then the string but um yeah i need to look hopefully there's a result with the django docs let's see allow using enum class i think this is what i want student yeah this is it um so this is like in a proposal but new feature fixed but how do i get to the actual docs django docs from it maybe if i search for django enum now okay here's choices so [Music] class here it is here it is okay so the numeration types do they have a yeah here so i can post this link in the chat here a little trivia question for chat what does type type okay uh hold it by question type all right so i guess i won't look up the look and the answer to that um let's see year in school and then models.txt choices this is it but i think i want integer choices i think they have a different one somewhere text choices here it is integer choices this is what i want models.integer choices so uh this is what i'm going to import from okay that's talking about that so question types oops um let's get question types so what type of questions would i have someone said multiple choice open-ended so i guess what i can do is multiple choice i think this is how i'll do it yeah i need to have a proper label there so i'll do multiple choice equals one and then um fill in the blank and then true or false okay and then choices here will be question types i think i need to do dot choices yeah okay so it gives me dot choices there and now i think i'm going to put this on two lines okay so question type and then that will be a model integer field and that will equal these choices and then oh wait these are all one so one two three okay um oh let me do short answer and then maybe i'll put short answer as the default that's really the only one that you can have so far um i'll have to make a different answers table if i really want to actually implement these different things but we'll we'll try it just just for filtering purposes we're going to add this field right now uh gotta use spring java spring um yeah i might be using that soon i thought i was gonna be using it right away but ends up the project that i'm working on i need to do react first and then i might be jumping over to java again yeah thanks for the follow tapayog how are you question typeset choices okay so now i need to actually run this migration oh duplicate values found what did i do oh these ones that's right okay that should work let me make sure my server runs again first okay my server runs now let me do python manage dot pi um make migrations okay and then python and there's a pie migrate let me just check that migration real quick yeah so you see what it did there it took the name of the variable and basically made that a tuple of the strings but the only reason why i did it as an enum kind of thing here instead of just a list of these i put it in a class instead because i can access them now by doing i can access the different types by doing like question types dot the name of it instead of it's easier to reference and know what it is throughout the code base rather than just doing a number because then every single time when i see that in the code base you know you have to look up what is that number and then you keep forgetting so this is a i think a cleaner way of writing choices there's javascript and python um i've used other languages i had to build something in go once and i used php a long time ago i probably don't even remember any php and then i worked in java for like seven months or so um i've dabbled in other languages but yeah most of my jobs have done javascript and python i did a little bit of c recently and then i was like ah this is so hard because i had didn't have all my regular methods it makes you appreciate python a lot when you work and see i wasn't working at a work work but i did some projects in it um okay so i have that i have my migration that's fine um actually migrate it oh something happened i got some kind of an error okay so i tried to run this sql query and i couldn't do it why i wonder if this has anything to do with okay not implement implemented all yes i feel like i had this problem before with add column question type not no um okay let me just try because i don't actually need those other questions i don't have a database client open so i'm just going to use the django here so what if i get rid of all my questions just delete them oh my server's not running let me run my server okay now i can go back into here and now i will try to run migrations no okay um i remember i got this error before and it seems to be something that like this would work if i was using postgres with django but some kind of specific thing that i need to fix with manga does support sql um if you want to use sql i don't think you would be using in the first place um no i think it just prints out a sequel but i think it's supposed to like migrations are supposed to run with or i think django makes some work with them because the initial migration does work like this works to set up that table and everything in because i have set up um where is it in my settings if i go to database i have mongodb set up here so um but i feel like there are some queries that don't work with jungle yet like maybe this ad field maybe ad field doesn't work yeah there is sql in the error message okay so i could try i think maybe what i'm gonna do is since this migration hasn't run yet i'm gonna delete it and then for cards i'm actually going to i don't think anything depends on this migration maybe dex does let me look at decks no no dependencies okay so i think what i can do is just roll back migration and then rerun and re-create an initial migration so um i'm typing get okay python managed pie i'm going to undo that all cards migrations so i'm going to migrate cards the card zap and then do zero and see it unapplied that initial migration from cards so now i can delete this file because this only has id question answer and deck but if i recreate the file it's going to have every field on it so i deleted that initial migration so now let's see okay so now i'm going to recreate an initial migration but including the new field so i'm going to do make migrations okay and that created a new initial migration okay it does have a dependency of the decks but that's okay dex doesn't really have anything on it um so okay id question answer question type now it's included in the initial migration now actually id here mongo's gonna create its own id anyway so actually confusingly there are two ids now and i really should look up how to turn that off maybe i will right after this but you'll see when i um maybe i'll pull i'll open up a client my mongodb client real quick let me just run that migration first migrate okay this version of jungle these are all just jungle warnings that's fine but it did run this migration now so now if i do python manage a pi run server it should run okay now if i add yeah now it gets question type okay that worked basically seems to create run the initial um query to to um to create this table and these fields and everything but whenever you update something it seems to just or not want to do exactly how you want it to do which is fine i mean the orm wasn't initially built for no sql so it's kind of fuzzy how it works there let me try opening compass where is my compass app okay mongodb compass hopefully okay that's going to take a second to load needs yeah it does i was wondering if i could use the underscore id if maybe for django i could just set the default id to look for underscore id instead of what it normally does like the auto incrementing id let me try to connect okay so i have this space learning app that's what i'm working off of and let's see cards card this is what i have so i need to add data here to look at it so let me add a test question i'll do it in the test deck short answer okay so now i have one question let me refresh refresh okay yeah see how can i say this yeah cool so see how it has the id like the underscore and then it has the regular id um i would like to get rid of this auto incrementing id and then use the id like you would normally in a in a database um i guess i've never thought about how to do that before because i've always used postgres with django so let me see actually um jingle jungle the library jungle how to remove duplicate ids no no it's just it's not coming up with any mongodb mongodb um change to underscore id disabling auto indexing oh this is weird change underscore id to id i don't know if i want to do that um but then if i turn off auto indexing a lot of built-in stuff in django looks for that as the primary key or i guess i set my own primary keys sometimes so i guess it doesn't matter right i could set anything as a primary key so yeah let me try that let me thanks for the suggestion disabling auto indexing django let's see our generate name perform keys logging let me try searching google for this one that's a lot of times i feel like i'm not getting good search results does jingle automatically generate indexes let me look at model index reference no yeah i don't know let me try one more time otherwise i might just leave it like it is it's not it's not uh hurting me at all so let's do django mongodb and let me think um change to underscore id is it possible to override no change to id let me see what this is when i'm using django and engine now that i'm not using the same thing as him probably that was too old i cannot correctly set primary key finally i created another field id um this time i can create from both web page and admin panel primary key equals object id yeah this is something i have to think of maybe because let me see if there's just an easy way to like uh django set well i can always set the primary key just like uh this i can set just any kind of an id and then it'll automatically give me an id but then um i wonder if that would fight with mongodb though let me try on their issues they have newer issues let me look for primary key filtering admin is not taking the id field as the default yeah that's what i want i want it to set as the default so let's see this person is saying a simple model with one field admin says the model properly but the list view is looking for the default id field it is not overwritten by that underscore id well how is he trying to do underscore id because he only has name this will automatically give you an id field yeah i don't know what he's trying to do okay apparently there was no solution for that and i wish he had showed some code but he didn't oh wait db index maybe this is it one more try db index equals true no okay never mind i think that's just not what i'm looking for anyway okay well if anyone figures that out i would love to know about it i might try to search for the answer later but just not live um okay so anyway i want to get some filtering in the app now that i have that other field which let me put this over here so now that i have the other field set up the question type so i want to be able to filter based off the question type so actually first let me just add different question types just random types um javascript and then deck and these can be from any deck i guess because i'm filtering based off of type so that is python let's do multiple choice python okay let me have one more multiple choice and then python okay so i'm gonna try to implement filtering here and this is a third-party package that i think is linked from here so rest framework does have some built-in filtering options that wait requests and responses oh here res framework does have some built-in filtering options that work pretty well but they don't seem to be quite what i ever need for what i'd ever need for a project so oh thanks for the fallout henry francois is that what you pronounce it um i remember from studying french that was so long ago i don't really remember anything generic filtering yeah so it has all these filtering but there's this library django filter which is just awesome generic reusable application to alleviate writing somewhere more of the mundane bits of view code so it does a lot of things for you and then you can set some filter back ends basically so let me look at let's see first installation let me install the app oh cool awesome uh okay let me install the app uh with poetry it's a poetry ad jingle filter by the way if you're just joining i wanna post the link to the repo that i'm working off of build with a quiet age oh poetry is a python package manager that it's really nice it gives me a lock file for dependencies and peer dependencies and things and then all of my main dependencies are in this toml file which is a python standard so this file isn't just used by poetry it's used by a lot of modern python tooling and there's a pep standard for this i can't remember which one it is but it's one of them so [Music] yeah you can look up pi project optoma file anyway so this is all my main dependencies that i've installed and of course there would be dev dependencies if i had any of those installed so now i have django filter so i need to set this up in my application and let me close this here's django filter let me move this over here okay so i need to add it to my installed apps so let me go to settings and where are my installed apps so um okay django filters and then i need to go to get started let's see this is just showing creating a model and then showing how to filter off that model but this is creating a filter class and basically i want to use my views and just set a few filter options so let me see fields filter let's see employee filter meta view um oh here it is integration with drf so drf is django rest framework that's what i'm using so yeah i'm going to be using these attributes these filter attributes so let me zoom in um by the way here's what i'm looking at if you want to look at it coding in rust cool i like russ island i don't know maybe 75 through the rest book before and i kept wanting to use it for i guess real projects but i never got around to it one of these days i will um okay so filter so there's a bunch of options that i can set on my views now and this shows list api view but i think i can use it in many different views and different kinds of views because uh generics a lot of the views if you look at the code like the source code for rest framework a lot of the views import from the same base classes so yeah i'll figure it out though but if i have to change my view sets i will but let's see filter back and filter set fields so let me go into cards now i'll stay out of decks for a while so now my cards view on my cards visa i want to add some of those filter attributes and django filter back-end yeah okay so i can set my back-ends and i can create custom back-ends too with like custom filtering options which i'm actually doing at work but here it's such a simple app i think i'll just use whatever standard built-in ones they have okay i can set this as the default filter back-end too where do i import okay from django filters import rest framework as filters let me go ahead and import that alright so let me go there and give a space okay and so now i have a filter back end which when i demo this it'll be more clear what i'm doing i'm setting up filters for django rest framework right now um camo so category and in stock let me see what fields i want to filter off of and i think i'll just do that one field right now let's see admin yeah models and i think question would be more of a search field or a sort field and answer i can just skip but question type is what i want so the only field i really want here is this one okay and let me try that so now oh yeah let me run my app first run server okay the server runs okay i have my thing installed i should be able to um just run this actually if i had um what is that api viewer called i can't remember the name of it swagger if i had swagger set up i think it would be easier to see this but oh well so now i can put query params decks get rid of that so on the cards endpoint i'm going to query for question type and i think question type can equal one maybe sweet cool so question type equals one let's see about two so two yay filtering works that's good okay so i can filter based off of the question type and this is pretty easy to set up in jingle best framework yeah postman postman is definitely nice um i have postman set up pretty nicely on my work computer not on this laptop so i do have postman here though but um let's see what am i oh yeah search fields so they have filter fields and filter sets or filter back-ends and filter set fields oh thanks for the follow hell hello iron how are you um search search filter user list oh yeah search field here it is um this is what i want so i'm gonna set search fields now because i do want to search by the name the question so if you want to search for a specific question you can so i'm going to just say oh this i guess i don't need the parentheses for a tuple but just makes it a little nicer okay so search fields and i'll search by question and let's see let's see if this works so question and question type so question i don't know maybe i can just put java oh it got back everything question equals oh oh right um it's ignoring the query parameter because i um put question equals and i don't have custom query parameters set up so i have to do search equals that's what i need to do okay it didn't work but that's okay um maybe it's not search maybe that's not what i'm trying to do okay i have this wrong um so it's setting let me look for search hmm okay so search docs here's search fields yeah it says search equals so it should be able to search oh oh i need the search filter as a filter back-end because right now i'm just doing um jingle filter back-end so i do need to tell it that i want to search so let me do that yeah a lot of things are way easier than in django um the coding school that i'm teaching at teaches express in the so first it teaches like javascript and then react and express and all that stuff and then at the very end of the program it gets to python and django so it kind of gets to a more robust comprehensive framework after you after you set everything up yourself so i think it is good to learn express and then use django for client apps because it makes things easier so okay uh jingle filters rest framework has no attribute search filter okay oh oh oh because this is just from rest framework itself right so how am i gonna do this because the names clash now so um let me actually just import rest framework.filters and import it directly for this one so i'll import a search filter and then oh wait i'll do oh yeah okay and then that should work let me see no module named rest framework dot filter um oh filters must be filters right yeah okay i just said it's spelled wrong okay so let me try searching yay and now it searches so let me try python cool test awesome so now i can search and filter on this and actually this isn't i thought all of these things were going to be different using but actually they're the exact same because i'm just using django standard ways of doing things all right so isn't too different i think where it would get different using is where i'm setting up if i'm doing custom indexes or something like that and then also was something else here i can't remember what it was and i was reading through the documentation um i think it was like complex queries or something constraints yeah there's quite a few things you can import from django actually i think my models are imported from django so if i go to models apps.jango.db okay nevermind i'm just using the default django models but i think there are models and things you can import from mongodb oh yeah if you want to create object ids and like specific things alrighty searches and filters fun um i mean they use the django orm um under the hood i think for all of the searching and filtering because when i build cut when i create my own custom filters i'm just using the regular django orm to interface with the database um and i i try to never do anything without using an orm with the database unless i'm absolutely sure that there's going to be no security flaw there um okay so filter set field search fields i wonder if there's anything else there's so many other things i can do like custom fields with django but this might be a good place to leave this app since it's not a long term project it was more of an example app to get working with um with django oh yeah ordering filter let me do this one because this is so useful and you can order ascending or descending yeah let me do the ordering filter and this just comes from straight up um the django rest framework too so actually this only takes a second to set up because i can just add this to my filter back ends okay so let me oh yeah that's right jongo handles converting all of it to queries i actually haven't really looked at the source code for jongo yet i guess i would if i used it in a bigger project maybe um what was i doing oh yeah ordering so let me order pass in some fields to order buy first let me just get everything back real quick okay so what would i want to order by let me try ordering by i don't know uh question yeah i'll just order by question so i think i can just do ordering equals question cool and it ordered uh alphabetically by the question now and if i wanted to change it to descending or ascending the opposite way i can do minus yeah and now it's the opposite basically and then i could do a second one i think i can just add a comma here and do like answer yeah and then i can just keep adding fields to order by order by this field and then this field and then that field so all of that stuff is pretty easy to set up oh my dot files yeah um i have a decent vim setup in my terminal there's probably a few things i need to update in those dot files but overall i'm happy with it um and then i don't i don't actually know if i have my oh my zish in there if that have that config um let's see filter when i get my new computer i'm planning on doing a video of everything that i set up on it okay so i have search filter ordering that's all pretty much the standard stuff this app is too simple for custom much custom work custom filtering or anything a default ordering yeah i guess i could specify a default ordering here if i wanted to or i guess it's just called bordering or not let's order equals and then the order would be question type maybe oh it is ordering it's getting late okay so now if i refresh yeah now it switched it around because it's ordering by the question type so it starts at one and then two and then four instead of the default ordering being the id which is what it normally is all right cool oh hey sam griffin you always read so late yeah so i've just been working on um more actually django with i got this third endpoint set up so i tried some filtering and stuff i was hoping to do some more specific code but actually jongo the mongodb library handles everything under the hood so i'm writing the same django i always write the same thing i would write if i was doing this with postgres so it's not that super clever or interesting or anything so yeah that's the repo again um so if you want like if you look at the repo i have two other repos that you can compare it to so i did it with postgres here and then i did a graphql postgres version of the same thing here so a bunch of different apps the same app built with different technologies which i think makes it kind of nice to compare yeah i love python django jingo is such a breath of fresh air and web development using python um 100 python isn't that awesome okay so i pretty much did that was all that i like had a goal of getting done in this stream there are a few things that i wish were better so if you look at these objects here so you know in django it adds this auto incrementing id field for me and that's what it uses for the primary key like if you're using um all sorts of things in django like the rest framework router all of that stuff uses the id field i guess it uses whatever primary key you set but the default is this but then gives me this underscore id so i couldn't find a good way to merge the two like sets django's as this and then use it for everything or set mongos as this you know they didn't want to work together but if anyone knows how to fix that in the future i would love to know about it i'm sure there's a way somehow work some stuff out like what you're having django issues okay so um i got filtering done that was pretty good um validators yeah i think actually sorry you didn't get to see the interesting part of the stream but it'll be live for a while um i think that's all that i wanted to do or get done let me go ahead and push my code here so okay status and i want to say that um i'm coming up with a new idea so next week i want to do another python stream uh you know every sunday night i think this is a good time for me but i don't know if it's going to be django because i've been doing like uh i don't know 10 months of django stream so i'm thinking about trying a new framework so i would like to give you or or i'd like to get your recommendations for a new framework so if you want to post recommendations for me in my discord oh what am i doing i'm so tired discord flask okay i used to use a lot of flask um there's a couple there's like the responder framework which i tried that's super interesting with some async stuff in python and of course there's like big frameworks like pyramid flask those kind of frameworks and then there's was it fast api i've never actually used that but people have been recommending it to me so i might check that one out too um maybe i'll take a vote on it okay what am i doing now these are all modified files all of these so i'm just going to do am and then added let's see what did i add today added uh card filtering by deck also filtering cards i guess filtering let me just do filtering searching and sorting that's good okay and that should push oh wait pull oh because i updated the readme it's a pretty safe pull okay so now i can push go bottle uh that's interesting i haven't tried bottle oh there's another small oh cherry cherry pie i think i tried that a few years ago that was fun too yeah i haven't used bottle i would be open to doing that uh static pages would be hyper useful with info from multiple models i'm not sure what you mean like um an explanation for the api you mean like api documentation maybe i'm not catching what you mean okay so i think that's it for the stream tonight sorry you just rated and then i didn't get to do a whole lot but i think actually tonight i feel like rating someone else so static pages oh right right okay so actually if you want to look um so the repo i was working on tonight send us down the raid chain yeah uh the repo i was working on tonight i do have a front end for it so i have this vue.js application so i haven't worked on it in a few months but there's a lot of it set up so it's supposed to interface with the django rest framework and yeah that's the front end for it so you can check that out if you want to run both and try them out together you
Info
Channel: Faraday Academy
Views: 26,462
Rating: undefined out of 5
Keywords: django with mongodb, djongo library, django app development, how to use django with nosql, django djongo, django, djongo
Id: mUPQii90K5A
Channel Id: undefined
Length: 176min 53sec (10613 seconds)
Published: Tue Feb 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.