class Account(models.Model):
holds_ready = models.IntegerField(max_length=8)
fines = models.DecimalField(max_digits=8, decimal_places=2)
next_due = models.CharField(max_length=10, null=True, blank=True)
def json_data(self):
return {
'next_due': self.next_due,
'holds_ready': self.holds_ready,
'fines': self.fines
}
运行python manage.py shell:
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:16:52)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import ...
>>> account = get_account("jave")
>>> print account.next_due
2014-05-27
>>> print account.holds_ready
1
>>> print account.fines
5.35
>>> a = account.json_data()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Account' object has no attribute 'json_data'
有人可以解释为什么json_data方法无法识别吗? 我搜索了现有的帖子。但找不到答案。
>>> print dir(account)
['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', u'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_base_manager', '_default_manager', '_deferred', '_do_insert', '_do_update', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', '_save_parents', '_save_table', '_set_pk_val', '_state', 'account_id', 'clean', 'clean_fields', 'date_error_message', 'delete', 'fines', 'full_clean', 'holds_ready', 'id', 'items_loaned', 'name', 'next_due', 'objects', 'parent_account_id', 'pk', 'prepare_database_save', 'root_account_id', 'save', 'save_base', 'serializable_value', 'sis_account_id', 'unique_error_message', 'validate_unique']