我在网上看到过人们使用__getattr__
Django 模型的例子,但每当我尝试时都会出错。(Django 1.2.3)
我__getattr__
在普通物体上使用时没有任何问题。例如:
class Post(object):
def __getattr__(self, name):
return 42
工作得很好...
>>> from blog.models import Post >>> p = Post() >>> p.random 42
现在,当我尝试使用 Django 模型时:
from django.db import models
class Post(models.Model):
def __getattr__(self, name):
return 42
并在解释器上进行测试:
>>> from blog.models import Post >>> p = Post() ERROR: An unexpected error occurred while tokenizing input The
以下回溯可能已损坏或无效错误消息是:('EOF in multi-line statement', (6, 0))
-------------------------------------------------- ------------------------- TypeError
Traceback(最近一次调用最后一次)/Users/josh/project/ in ()
/Users/josh/project/lib/python2.6/site-packages/django/db/models/base.pyc in init (self, *args, **kwargs) 338 if kwargs: 339 raise TypeError("'%s ' 是此函数的无效关键字参数" % kwargs.keys()[0]) --> 340 signals.post_init.send(sender=self.class , instance=self) 341 342 def repr (self):
/Users/josh/project/lib/python2.6/site-packages/django/dispatch/dispatcher.pyc in send(self, sender, **named) 160 161 for self._live_receivers(_make_id(sender)) 中的接收者: --> 162 response = receiver(signal=self, sender=sender, **named) 163 response.append((receiver, response)) 164 返回响应
/Users/josh/project/python2.6/site-packages/photologue/models.pyc in add_methods(sender, instance, signal, *args, **kwargs) 728 """ 729 if hasattr(instance, 'add_accessor_methods') : --> 730 instance.add_accessor_methods() 731 732 # 将 add_accessor_methods 函数连接到 post_init 信号
TypeError:“int”对象不可调用
有人可以解释发生了什么吗?
编辑:我在示例中可能过于抽象,这里有一些更接近我在网站上实际使用的代码:
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField()
date_published = models.DateTimeField()
content = RichTextField('Content', blank=True, null=True)
# Etc...
Class CuratedPost(models.Model):
post = models.ForeignKey('Post')
position = models.PositiveSmallIntegerField()
def __getattr__(self, name):
''' If the user tries to access a property of the CuratedPost, return the property of the Post instead... '''
return self.post.name
# Etc...
虽然我可以为 Post 类的每个属性创建一个属性,但这会导致大量代码重复。此外,这意味着每当我添加或编辑 Post 类的属性时,我都必须记住对 CuratedPost 类进行相同的更改,这似乎是代码腐烂的秘诀。