Python论坛  - 讨论区

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

2007年04月24日 星期二 23:51

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 "", line 1, in 
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 "", line 1, in 
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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年04月25日 星期三 09:20

黄毅 yi.codeplayer在gmail.com
星期三 四月 25 09:20:10 HKT 2007

        self.taginfo.count += 1
        self.taginfo.save()
        super(TagMap,self).save()

出错的语句是第一句,应该没有对应的 taginfo,你试试把 super...save()  那句提前。


On 4/24/07, Brightman <mr.brightman at 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
>
>
> 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 "", line 1, in 
>   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 "", line 1, in 
>   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 at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070425/1653030a/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年04月25日 星期三 10:09

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 "", line 1, in 
  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 
> 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 "", line 1, in 
>       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 "", line 1, in 
>       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
>     python-chinese在lists.python.cn>
>     Subscribe: send subscribe to
>     python-chinese-request在lists.python.cn
>     python-chinese-request在lists.python.cn>
>     Unsubscribe: send unsubscribe to
>      python-chinese-request在lists.python.cn
>     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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年04月25日 星期三 10:56

bbenyu bbenyu在gmail.com
星期三 四月 25 10:56:28 HKT 2007

´ó¼ÒºÃ!
    ÏàÐÅ´ó¼Ò¿Ï¶¨¿´¹ý ku6.com »ò youtube.comºÍ tudou.com ÀàËÆÍøÕ¾°É?
Ä¿Ç°ÊÖÍ·ÉÏÓиöÐèÇó,¾ÍÊÇ¿ª·¢ÀàËÆ(¿ÉÒÔ˵һÑù)µÄÍøÕ¾.
    ²»ÏþµÃÄÄλÓÐÐËȤ?Çë»Ø¸´!

×¢:ÇëÈÏÕæÁ˽âÈ«²¿ÐèÇóºÍÉè¼Æ·½°¸ºó,ÔÙ×÷³ö»Ø¸´!
     ²¢Ð´Ã÷:¹À¼Æ¿ª·¢Ê±¼ä¡¢±¨³ê£¡
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070425/13a86a2a/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年04月25日 星期三 18:05

doudou doudou array.doudou在gmail.com
星期三 四月 25 18:05:58 HKT 2007

¶¼ÊÇÉÏǧÍòÃÀ½ð¶Ñ³öÀ´µÄÍøÕ¾£¬Èç¹ûûÓÐ×ã¹»µÄ×ʽ𣬸ù±¾¾ÍûÓоºÕùµÄ»°ÓïȨ£¬ºÎ±ØÄØ¡£ÊÓƵÊг¡¿ÖÅÂÖ»ÓÐÒ»Á½ÄêµÄ×ÔÓɾºÕùÆÚÁË£¬»¹ÊDz»Òª¸ú·çÁË°É¡£
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070425/06d140d2/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年04月26日 星期四 14:56

老生 liangzhengzhang在gmail.com
星期四 四月 26 14:56:24 HKT 2007

È˼ÒÕûÒ»¸ö¼¼ÊõÍŶÓÔÚÖ§³Å£¬Â¥Ö÷Òª¿ª·¢ÕßÒ»ÏÂд³ö¿ª·¢Ê±¼ä¼°±¨³êÕâ¸ö¹À¼ÆºÜÄÑ£¬

ûÓÐÒ»¸ö¹¦ÄܵÄÉóÊÓ±ê×¼£¬Ëµ´óÒ²´ó£¬ËµÐ¡Ò²Ð¡£¬

Èç¹ûÂ¥Ö÷ÊǸɼ¼ÊõÕâÐгöÉíµÄ»°ÏÈд¸ö´ó¸ÅµÄ¹¦Äܱê×¼°É£¬Èç¹û²»ÊǼ¼Êõ³öÉíÄǾÍÏÈÉîÈëÁ˽âËûÃǵļܹ¹°É£¬·ñÔòºÜÈÝÒ×±»ºöÓƵġ£¡£¡£¡£


ÔÚ07-4-25£¬bbenyu <bbenyu在gmail.com> дµÀ£º
>
>  ´ó¼ÒºÃ!
>     ÏàÐÅ´ó¼Ò¿Ï¶¨¿´¹ý ku6.com »ò youtube.comºÍ tudou.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/20070426/38b90483/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年04月26日 星期四 15:02

老生 liangzhengzhang在gmail.com
星期四 四月 26 15:02:11 HKT 2007

ÓÃdispatcher.connectÓ¦¸ÃÄܽâ¾öÄãµÄ×îÖÕÄ¿µÄ°É£¿

Õë¶ÔmodelsµÄÿ¸ö²Ù×÷¶¼»áÓÐÏà¶ÔÓ¦µÄ±£´æ¡¢É¾³ý¡¢ÐÞ¸Äʼþ£¬Èç±£´æÊÇsignals.pre_save


ÔÚ07-4-24£¬Brightman <mr.brightman在gmail.com> дµÀ£º
>
>
> ÕâÊÇ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 "", line 1, in 
>   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 "", line 1, in 
>   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
> 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/20070426/b97595ce/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号