[python-chinese] 请教python中super的用法

Brightman mr.brightman在gmail.com
星期二 四月 24 23:51:33 HKT 2007


ÕâÊÇdjangoÖеÄÒ»¸ö¼òµ¥Ó¦Óã¬Ï£ÍûÔÚTagMapµÄsave deleteÐÐΪÖÐÐÞ¸ÄTagInfoµÄ
¼ÆÊý¡£
class TagInfo(models.Model):
name = models.CharField(maxlength=50)
count = models.IntegerField(default=0)
def __str__(self):
return self.name


class TagMap(models.Model):
taginfo = models.ForeignKey(TagInfo)
content_type = models.ForeignKey(ContentType)
object_id = models.IntegerField()
content_object = models.GenericForeignKey()

class Meta:
ordering = ["taginfo"]
def __str__(self):
return self.taginfo.name
def save(self):
self.taginfo.count += 1
self.taginfo.save()
super(TagMap,self).save()
def delete(self):
self.taginfo.count -= 1
self.taginfo.save()
super(TagMap,self).delete()

»ã±¨ÈçÏ´íÎ󣬶ÔpythonµÄsuperÓ÷¨²»Ê죬ÏÖÇë½Ìһϣ¬Ð»Ð»£¡
>>> tag1=TagMap(taginfo=fatty,content_object=food)
>>> tag1.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "D:\project\superstar\..\superstar\apps\forum\models.py", line 38,
in sav
e
self.taginfo.count += 1
File "C:\Python25\Lib\site-packages\django\db\models\fields\related.py",
line
165, in __get__
raise self.field.rel.to.DoesNotExist
DoesNotExist
>>> tag1.taginfo
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python25\Lib\site-packages\django\db\models\fields\related.py",
line
165, in __get__
raise self.field.rel.to.DoesNotExist
DoesNotExist
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070424/57d8c916/attachment.html 


关于邮件列表 python-chinese 的更多信息