jarmod
jarmod

Reputation: 78840

Django use model EmailField raising AttributeError

I've added a model boolean field called 'is_dotcom' to my admin list_display, and the implementation is:

email = models.EmailField(max_length=254)

def is_dotcom(self):
    return self.email.lower().endsWith(".com")

is_dotcom.admin_order_field = 'email'
is_dotcom.boolean = True
is_dotcom.short_description = 'Company?'

But all this yields on my admin page is "(None)". I'm expecting True/False (though sometimes my booleans show as a green check or red no entry sign, anyone know why that is?)

I've based this code on an example in the django tutorial.

I'm assuming that "(None)" is being shown because the is_dotcom() method is raising an AttributeError which django is catching. I'm guessing that it's legal to call .lower() on an EmailField but I don't know for sure (what do you guys do for reference documentation?) Thanks.

Upvotes: 1

Views: 896

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169494

The problem is in this line:

    return self.email.lower().endsWith(".com")

The method is .endswith().
Please note the absence of camel-case.

A simplified example which reproduces the error:

>>> 'foo'.endsWith('test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'endsWith'

Upvotes: 2

Related Questions