Django使用模型EmailField引发AttributeError
我已经在我的管理列表显示中添加了一个名为“is_dotcom”的模型布尔字段,其实现是:Django使用模型EmailField引发AttributeError,django,django-models,django-admin,Django,Django Models,Django Admin,我已经在我的管理列表显示中添加了一个名为“is_dotcom”的模型布尔字段,其实现是: 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 =
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?'
但我的管理页面上的所有这些结果都是“(无)”。我期待着真/假(尽管有时我的布尔人会显示为绿色支票或红色禁止进入标志,有人知道为什么吗?)
我基于中的一个示例编写了这段代码
我假设显示“(None)”是因为is_dotcom()方法正在引发django正在捕获的AttributeError。我猜在EmailField上调用.lower()是合法的,但我不确定(你们在参考文档方面做了什么?)谢谢。问题出在这一行:
return self.email.lower().endsWith(".com")
方法是。请注意没有骆驼箱 再现错误的简化示例:
>>> 'foo'.endsWith('test')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'endsWith'
>>'foo'.endsWith('test'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“str”对象没有属性“endsWith”
问题出在这一行:
return self.email.lower().endsWith(".com")
方法是。请注意没有骆驼箱 再现错误的简化示例:
>>> 'foo'.endsWith('test')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'endsWith'
>>'foo'.endsWith('test'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“str”对象没有属性“endsWith”
谢谢!诅咒这些新型的非编译语言;-)还意识到为什么我的一个布尔列显示的是True/False而不是绿色的check/red no entry符号-我没有为该列设置field.boolean=True。需要更多的咖啡!谢谢诅咒这些新型的非编译语言;-)还意识到为什么我的一个布尔列显示的是True/False而不是绿色的check/red no entry符号-我没有为该列设置field.boolean=True。需要更多的咖啡!