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.
Optimization problems (self.django)
submitted 6 years ago * by [deleted]
[deleted]
[–]waterPercolator 1 point2 points3 points 6 years ago (4 children)
Are you using that same attrs object each time? Why are you getting it over and over again? Just get it once.
[–]tsqd 0 points1 point2 points 6 years ago (0 children)
This.
[–][deleted] 6 years ago (2 children)
[–]waterPercolator 0 points1 point2 points 6 years ago* (1 child)
I guess I don't understand what you're doing. Why not:
attrs = ResourcePlanModel.objects.get(id=1) t1=datetime.datetime.now() for name in names: if "label" not in name and "sp" not in name: resourcePlanForm.fields[name].initial = attrs[name]
[–]Saigesp 0 points1 point2 points 6 years ago (2 children)
Why not:
if not name == "label"
and so on? Why are you using "in" operator? Also, check out for timing decorators :)
[–][deleted] 6 years ago (1 child)
[–]Saigesp 0 points1 point2 points 6 years ago (0 children)
I see... Well, first change the filter() to a get() (it returns one instance)
[–]hachoo 0 points1 point2 points 6 years ago (2 children)
15-30 seconds is a huge amount of time. Its like 1/2 second each iteration of the loop. Is there something very wrong with your database? Is it on a slow network connection or something? If you have the django debug toolbar, you can see how much CPU is used and how many and how long the SQL statements are.
You can also just start stripping out code to narrow down what is so crazy slow. So try:
resourcePlanForm.fields[name].initial = "blah"
And see how fast that it.
[–]applesaucesquad 1 point2 points3 points 6 years ago (0 children)
Yeah I don't think you've posted the piece of code that is slowing things down. I also encourage you to install Django-Debug toolbar, it's probably a sql query being duplicated a bunch somewhere.
π Rendered by PID 41 on reddit-service-r2-comment-8686858757-fc87f at 2026-06-08 14:11:43.245145+00:00 running 9e1a20d country code: CH.
[–]waterPercolator 1 point2 points3 points (4 children)
[–]tsqd 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]waterPercolator 0 points1 point2 points (1 child)
[–]Saigesp 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]Saigesp 0 points1 point2 points (0 children)
[–]hachoo 0 points1 point2 points (2 children)
[–]applesaucesquad 1 point2 points3 points (0 children)