0

I have this error

Name 'Order' is not defined

in the following claue:

            try:
                order = Order.objects.from_request(request)
                cart = OrderCart(order)
            except Order.DoesNotExist:
                pass

Order is defined exactly in the same file and is subclass of model.Model

To be more concrete, here is the file I'm working with: https://bitbucket.org/chris1610/satchmo/src/a04c87a539f3/satchmo/apps/satchmo_store/shop/models.py with exception occuring in line 242

Why can this be?

Update:

Here is the traceback:

Traceback:
File "/home/evgeniuz/.virtualenvs/port/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/home/evgeniuz/src/chillifish-pg/lib/payment/views/balance.py" in balance_remaining_order
  30.         template='shop/checkout/balance_remaining.html')
File "/home/evgeniuz/src/chillifish-pg/pg/pgstore/utils.py" in _view
  38.             return view_func(request, *args, **kwargs)
File "/home/evgeniuz/src/chillifish-pg/lib/payment/views/balance.py" in balance_remaining
  66.         'paymentmethod_ct': len(config_value('PAYMENT', 'MODULES'))
File "/home/evgeniuz/.virtualenvs/port/lib/python2.7/site-packages/django/template/context.py" in __init__
  149.             self.update(processor(request))
File "/home/evgeniuz/src/chillifish-pg/lib/satchmo_store/shop/context_processors.py" in settings
  21.     cart = Cart.objects.from_request(request)
File "/home/evgeniuz/src/chillifish-pg/lib/satchmo_store/shop/models.py" in from_request
  237.                 except Order.DoesNotExist:

Exception Type: NameError at /shop/checkout/balance/1/
Exception Value: global name 'Order' is not defined
evgeniuz
  • 2,599
  • 5
  • 30
  • 36
  • 1
    Line 242 in that file is `except Order.DoesNotExist:`... why are you calling it Foo? – David Robinson May 14 '12 at 14:26
  • 1
    Can you double check the link you provided? I can't find `Foo` in the file, and line 242 contains `except Order.DoesNotExist` – Levon May 14 '12 at 14:26
  • "Foo" does not appear in your source. Please provide a complete, correct, self contained and minimal example: http://sscce.org/ – Marcin May 14 '12 at 14:26
  • Sorry, I wrote question to be more generic and then added this file. Edited question. – evgeniuz May 14 '12 at 14:30
  • I suggest that you try putting `assert Order` at the top of that method. That will tell you if `Order` was defined, and somehow became undefined, or not. – Marcin May 14 '12 at 15:25

2 Answers2

1

Maybe I'm just missing something, but you're referring to the Order class in your CartManager before it's defined. Order isn't defined until line 642. I don't know a lot about the inner workings of Django, but I would try moving the Order class up above CartManager and see if it gets you past this error.

patrickn
  • 2,501
  • 4
  • 22
  • 21
  • Python is an interpreted language, so the environment won't be searched for the `Order` class until that point in the code is reached. By that time, those models will already have been defined. – David Robinson May 14 '12 at 15:47
  • David, I hesitated posting the above answer knowing what you say about Python to be true. However, I've had this precise "not defined" error solved by placing the offending model above where it's being referred to. I fully accept what you're saying to be true, being a novice and procedural C-based programmer by experience. – patrickn May 14 '12 at 16:09
  • @DavidRobinson, From https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey : "If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself". In this case, I don't know how to reference Order in the CartManager (as opposed to in a model), but I would try patrickn's solution – Ben Rosnick May 14 '12 at 19:38
  • I'm curious as to whether or not this solution helped the OP. – patrickn May 15 '12 at 19:14
0

For future reference.

I think you forgot to import the Order Class