Forums

SQlite problem or something else (Django)?

Hellow! I have simple code that works on localhost but not working on PythonAnywhere:

models.py (...part of file...)

class Category(MainClass):
    class Meta():
        db_table = 'category'
    parentCategory = models.ForeignKey("Category", blank=True, null=True)

views.py (...part of file...)

args = dict()
all_categories = Category.objects.all()

def categories(request, cats=all_categories):
    main_categories = list()
    for cat in cats:
        if not cat.parentCategory_id:
            main_categories.append(cat)
            args['main_categories'] = main_categories

        else:
            args['main_categories'] = cats

    return render_to_response('categories.html', args, context_instance=RequestContext(request))

Idea is - to show categories without parentCategory property ( "main categories").

On my mac it filters by

if not cat.parentCategory_id:

enter image description here

On PythonAnywhere - not. Shows all categories... with parentCategory and without.

pip freeze on PythonAnywhere

Django==1.7.4
Pillow==2.7.0
django-admin-bootstrapped==2.3.2
django-appconf==0.6
django-bootstrap3==5.1.1
django-debug-toolbar==1.2.2
django-imagekit==3.2.5
django-simple-captcha==0.4.4
pilkit==1.1.12
six==1.9.0
sqlparse==0.1.14

pip freeze on local mac

Django==1.7.1
Pillow==2.6.1
django-admin-bootstrapped==2.3.1
django-appconf==0.6
django-bootstrap3==5.0.3
django-debug-toolbar==1.2.2
django-imagekit==3.2.5
django-simple-captcha==0.4.3
pilkit==1.1.12
pyserial==2.7
six==1.9.0
sqlparse==0.1.14

So, please, help me to understand the problem.

The code that you have does some filtering into main_categories and then, if the last category has a parent, it puts the entire list of categories into the args.

There are also 2 other general issues with your code:

  1. It's extremely inefficient to do filtering in Python. Look up the Django docs for filter to get the database to do the filtering.
  2. Because you get the list of categories outside of the request, you'll need to reload your web app every time you add a category.

Thank you glenn! I see my mistake now :)

As you advised i'v made all filtering with .objects.filter().

But i don't understand - 2. Because you get the list of categories outside of the request, you'll need to reload your web app every time you add a category. - you mean reload page or restart app on PythonAnywhere web console?

I mean you'll need to restart the app because the queryset all_categories will only be loaded when the app is started.

It was bad idea,iv fixed it, thank you!