Name 'DecimalField' is not defined

import collections

from decimal import Decimal

from email.policy import default

import imp

from multiprocessing.sharedctypes import Value

from django.shortcuts import render

from django.db.models import Q, F, Value, Func, ExpressionWrapper

from django.http import HttpResponse

from store.models import Customer, Order, Product, OrderItem

def say_hello(request):
discounted_price = ExpressionWrapper(F(‘unit_price’) * 0.8, output_field=DecimalField())
queryset = Product.objects.annotate(
discounted_price = discounted_price
)
return render(request, ‘hello.html’, {‘name’: ‘Mosh’, ‘result’: list(queryset)})

You have to import the DecimalField form django.db.models

hope I helped

3 Likes

If you are using any script file and getting "Uncaught ReferenceError: x is not defined " which means ‘x’ is either a variable or a method which you are trying to use before declaring it using var keyword. This means that there is a non-existent variable referenced somewhere. This variable needs to be declared, or you need to make sure it is available in your current script or scope otherwise , it will endup throwing this ‘x’ is not defined error . This usually indicates that your library is not loaded and JavaScript does not recognize the ‘x’.

To solve this error: Load your library at the beginning of all your scripts.

There can be multiple other reasons for this issue:

  • Conflict with Other Libraries
  • Path to your library included is not correct
  • Llibrary file is corrupted
  • Working offline (when you use CDN)

I tried this and was not enough, and I actually found a simple solution and I found it on the django website dictionary.

instead of writing:

output_field=DecimalField()

How Mosh teaches

you write:

output_field=DecimalField(max_digits=5, decimal_places=2)

Meaning you actually define the value

Works perfect!