[python-chinese] 请教python中super的用法
Brightman
mr.brightman在gmail.com
星期三 四月 25 10:09:13 HKT 2007
不好意思,说得不是很清楚。taginfo content_object都有的。
yellow=TagInfo.objects.get(name="yellow")
food=Forum.objects.get(name="food")
tag1=TagMap(taginfo=yellow,content_object=food)
>>> 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
黄毅 写道:
> self.taginfo.count += 1
> self.taginfo.save()
> super(TagMap,self).save()
>
> 出错的语句是第一句,应该没有对应的 taginfo,你试试把 super...save()
> 那句提前。
>
>
> On 4/24/07, *Brightman* <mr.brightman在gmail.com
> <mailto:mr.brightman在gmail.com>> wrote:
>
>
> 这是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 <http://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 <http://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
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> <mailto:python-chinese在lists.python.cn>
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> <mailto:python-chinese-request在lists.python.cn>
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> <mailto:python-chinese-request在lists.python.cn>
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> --
> http://codeplayer.blogspot.com/
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070425/af10799b/attachment.html
关于邮件列表 python-chinese 的更多信息