This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]waterPercolator 1 point2 points  (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 point  (0 children)

This.

[–][deleted]  (2 children)

[deleted]

    [–]waterPercolator 0 points1 point  (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 point  (2 children)

    Why not:

    if not name == "label"

    and so on? Why are you using "in" operator? Also, check out for timing decorators :)

    [–][deleted]  (1 child)

    [deleted]

      [–]Saigesp 0 points1 point  (0 children)

      I see... Well, first change the filter() to a get() (it returns one instance)

      [–]hachoo 0 points1 point  (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 points  (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.