G Ang
G Ang

Reputation: 25

Django: Field missing when queried

I'm having some trouble querying for my CalculatedAmt field in my database. In fact, according to the error code, the field does not even exist. However, I have included it in my Models.py and it can even be seen in my admin interface on Django.

Here are my codes:

Model.py - I didn't join the tables because I didn't need the join for my queries

class Customer(models.Model):
    Customer_Name_P = models.CharField(max_length=30, default="missing")
    Customer_TelNo = models.CharField(max_length=8, default="missing")
    Postal_Code = models.CharField(max_length=6, default="missing")

def __str__(self):
        return "%s" % self.Customer_Name_P

class Transaction(models.Model):
    DocRef = models.CharField(max_length=10,default="missing")
    DocDate = models.DateField()
    AcCrIsMinus1 = models.CharField(max_length=10,default="missing")
    AcCurWTaxAmt = models.CharField(max_length=10,default="missing")
    HomeWTaxAmt = models.CharField(max_length=10,default="missing")
    CalculatedAmt = models.CharField(max_length=10, default="missing")
    ProjectCode = models.CharField(max_length=10,default="missing")
    Location = models.CharField(max_length=10,default="missing")
    Sales_Person = models.CharField(max_length=10,default="missing")
    AcCode = models.CharField(max_length=8,default="missing")
    Customer_Name = models.CharField(max_length=30,default="missing")

    def __str__(self):
        return "%s" % self.id


class Salesperson(models.Model):
    Sales_Person_P = models.CharField(max_length=10,default="missing",primary_key=True)
    Sales_Person_Name = models.CharField(max_length=10,default="missing")
    Sales_Person_Contact = models.CharField(max_length=10,default="missing")

    def __str__(self):
        return "%s" % self.Sales_Person_P


class Account(models.Model):
    AcCode_P = models.CharField(max_length=8, default="missing",primary_key=True)
    Customer_Name = models.CharField(max_length=30,default="missing")
    AcCur = models.CharField(max_length=3, default="missing")

    def __str__(self):
        return "%s" % self.AcCode_P

Query in shell (for a table):

Transaction.objects.all().filter(Sales_Person=Sales_Person).values('DocRef','DocDate','AcCrIsMinus1','HomeWTaxAmt','ProjectCode','Customer_Name','Location')

Error Message:

django.core.exceptions.FieldError: Cannot resolve keyword 'CalculatedAmt' into field. Choices are: AcCode, AcCrIsMinus1, AcCurWTaxAmt, Customer_Name, DocDate, DocRef, HomeWTaxAmt, Location, ProjectCode, Sales_Person, id

Here's a screenshot of a Transaction instance from the admin interface: screenshot

Screenshot of the output for python manage.py showmigrations: screenshot

Screenshot of migrations 0001_initial file screenshot

Thank you!

Upvotes: 0

Views: 1050

Answers (1)

Rarblack
Rarblack

Reputation: 4664

This error does not say that the field is missing. The FieldError exception is raised when there is a problem with a model field. This can happen for several reasons:

  • A field in a model clashes with a field of the same name from an abstract base class
  • An infinite loop is caused by ordering
  • A keyword cannot be parsed from the filter parameters
  • A field cannot be determined from a keyword in the query parameters
  • A join is not permitted on the specified field
  • A field name is invalid A query contains invalid order_by arguments

Just for your information, for a field which does not exist there is a FieldDoesNotExist exception. It is raised by a model’s _meta.get_field() method when the requested field does not exist on the model or on the model’s parents.

The error is because you are fetching all() first then try to filter. When you fetch all you get all fields and then not include CalculatedAmt in your values.

t = Transaction.objects.filter(Sales_Person__iexact=Sales_Person)

t object will be containing all the field of Transaction so no need for .values, you can retrieve them by t.CalculatedAmt for example. P.s. ixact means exact matching without caring for case-sensitiveness.

Moreover, I assume you have tried to connect 2 models together with Sales_Person field. But you are doing it in the wrong way.

You should use one of the OnetoOne, ForeignKey or ManytoMany relationships. I will use ForeignKey assuming that a salesperson can have many transactions but each transaction can only belong to one person.

class Transaction(models.Model):
DocRef = models.CharField(max_length=10,default="missing")
DocDate = models.DateField()
AcCrIsMinus1 = models.CharField(max_length=10,default="missing")
AcCurWTaxAmt = models.CharField(max_length=10,default="missing")
HomeWTaxAmt = models.CharField(max_length=10,default="missing")
CalculatedAmt = models.CharField(max_length=10, default="missing")
ProjectCode = models.CharField(max_length=10,default="missing")
Location = models.CharField(max_length=10,default="missing")
Sales_Person = models.ForeignKey('Salesperson', on_delete=models.CASCADE)
AcCode = models.CharField(max_length=8,default="missing")
Customer_Name = models.CharField(max_length=30,default="missing")

def __str__(self):
    return "%s" % self.id


class Salesperson(models.Model):
    Sales_Person_P = models.CharField(max_length=10,default="missing",primary_key=True)
    Sales_Person_Name = models.CharField(max_length=10,default="missing")
    Sales_Person_Contact = models.CharField(max_length=10,default="missing")

    def __str__(self):
        return "%s" % self.Sales_Person_P

Upvotes: 1

Related Questions