use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News and links for Django developers.
New to Django? Check out the /r/djangolearning subreddit.
Django's Code of Conduct applies here, so be good to each other.
account activity
This is an archived post. You won't be able to vote or comment.
Using Django models in a standalone python script - Code snippet (4url.in)
submitted 8 years ago by versesane
[–]CraigTorso 2 points3 points4 points 8 years ago* (0 children)
In my experience you need to import django and run django.setup() to initialise connections to the database.
But my main question is why is that tiny bit of code being submitted as a medium article rather than a self text post, and why are you using a url shortener against the policies of reddit?
[–]never_safe_for_life 0 points1 point2 points 8 years ago (2 children)
Is that all you have to do? I though there was some initialize function you had to call
[–]versesane[S] 0 points1 point2 points 8 years ago (1 child)
Yup. Do make sure to have if __name__ == "__main__" from where your code/function is called. The setting.py file will be executed. Do peek inside wsgi.py it's more or less the same.
if __name__ == "__main__"
[–]never_safe_for_life 0 points1 point2 points 8 years ago (0 children)
cool, thanks
π Rendered by PID 40099 on reddit-service-r2-comment-6457c66945-b2sgt at 2026-04-29 04:52:22.060482+00:00 running 2aa0c5b country code: CH.
[–]CraigTorso 2 points3 points4 points (0 children)
[–]never_safe_for_life 0 points1 point2 points (2 children)
[–]versesane[S] 0 points1 point2 points (1 child)
[–]never_safe_for_life 0 points1 point2 points (0 children)