Python论坛  - 讨论区

标题:[python-chinese] [Django] 关于models.ForeignKey,请教

2007年02月26日 星期一 09:57

大郎 iexper在gmail.com
星期一 二月 26 09:57:54 HKT 2007

¶¨ÒåÁËÒ»¸öentryµÄmodel:

class entry(models.Model):
>     title = models.CharField(maxlength=200)
>     category = models.ForeignKey(category)
>     userid = models.ForeignKey(writer)
>     expert = models.CharField(maxlength=4000)
>     content = models.TextField()
>     PostDate = models.DateTimeField(auto_now_add=True)
>     PostIp = models.CharField(maxlength=20)
>     EditUserid = models.ForeignKey(writer)
>     EditDate = models.DateTimeField()
>     EditIp = models.CharField(maxlength=20)
>

»¹Óм¸¸öÆäËûÏà¹Ømodel

install appsµÄʱºò£¬Ìáʾ£º

Error: Couldn't install apps, because there were errors in one or more mode
> blog.entry: Accessor for field 'userid' clashes with related field
> 'writer.
> _set'. Add a related_name argument to the definition for 'userid'.
> blog.entry: Accessor for field 'EditUserid' clashes with related field
> 'wri
> ntry_set'. Add a related_name argument to the definition for 'EditUserid'.
>

ÎÒ¸ÃÈçºÎ£¿¶àл
-- 
ÎÒ×ßµ½Ò»¸öÄ°ÉúµÄµØ·½
¸æËß±ðÈË ÎÒҪȥÁ÷ÀË

Ŷ£¬ÎÒҪȥÁÆÉË¡­¡­

http://www.iexper.com/blog/
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070226/faac330c/attachment.html 

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

2007年02月26日 星期一 10:02

limodou limodou在gmail.com
星期一 二月 26 10:02:22 HKT 2007

On 2/26/07, 大郎 <iexper在gmail.com> wrote:
> 定义了一个entry的model:
>
> > class entry(models.Model):
> >     title = models.CharField(maxlength=200)
> >     category = models.ForeignKey(category)
> >     userid = models.ForeignKey(writer)
> >     expert = models.CharField(maxlength=4000)
> >     content = models.TextField()
> >     PostDate = models.DateTimeField(auto_now_add=True)
> >     PostIp = models.CharField(maxlength=20)
> >     EditUserid = models.ForeignKey(writer)
> >     EditDate = models.DateTimeField()
> >     EditIp = models.CharField(maxlength=20)
> >
>
> 还有几个其他相关model
>
>  install apps的时候,提示:
>
> > Error: Couldn't install apps, because there were errors in one or more
> mode
> > blog.entry: Accessor for field 'userid' clashes with related field
> 'writer.
> > _set'. Add a related_name argument to the definition for 'userid'.
> > blog.entry: Accessor for field 'EditUserid' clashes with related field
> 'wri
> > ntry_set'. Add a related_name argument to the definition for 'EditUserid'.
> >
>
> 我该如何?多谢

其为你有两个字段都是引用了writer,所以django会报错,其中一个需要使用related_name来定义一个名字,就是一个字符串,如related_name
= 'userid'

-- 
I like python!
UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

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

2007年02月26日 星期一 10:28

大郎 iexper在gmail.com
星期一 二月 26 10:28:05 HKT 2007

>
> userid = models.ForeignKey(writer)
> EditUserid = models.ForeignKey(writer, related_name='EntryUser')
>

ÎÒÊÇÕâÑù´¦ÀíµÄ£¬Èç¹ûÔÚÒ»¸ömodelÖÐÔÚÁ½¸öÒÔÉϵÄÍâ¼üÖ¸ÏòÁËͬһ¸ö±íµÄͬһ¸ö×ֶΣ¬ÄÇôÐèÒª½«ÆäËû±íÖ¸¶¨Ò»¸ö±ðÃû

ÊÇÕâÑùÂð£¿

ÁíÍ⣬»¹ÓöÉÏÒ»¸öÇé¿ö£º
ÔÚÕâ¸ömodelûÓÐÐÞ¸Ä֮ǰ£º

> userid = models.ForeignKey(writer)
> username = models.ForeignKey(writer.username)
>

Õâ¸öʱºòºÃÏñÒ²ÊÇÏà·ÂµÄÌáʾ¡£
ÊÇ·ñʹÓÃÒÔÉϵÄͬÑù´¦Àí°ì·¨£¿

On 2/26/07, limodou <limodou在gmail.com> wrote:
>
> On 2/26/07, ´óÀÉ <iexper在gmail.com> wrote:
> > ¶¨ÒåÁËÒ»¸öentryµÄmodel:
> >
> > > class entry(models.Model):
> > >     title = models.CharField(maxlength=200)
> > >     category = models.ForeignKey(category)
> > >     userid = models.ForeignKey(writer)
> > >     expert = models.CharField(maxlength=4000)
> > >     content = models.TextField()
> > >     PostDate = models.DateTimeField(auto_now_add=True)
> > >     PostIp = models.CharField(maxlength=20)
> > >     EditUserid = models.ForeignKey(writer)
> > >     EditDate = models.DateTimeField()
> > >     EditIp = models.CharField(maxlength=20)
> > >
> >
> > »¹Óм¸¸öÆäËûÏà¹Ømodel
> >
> >  install appsµÄʱºò£¬Ìáʾ£º
> >
> > > Error: Couldn't install apps, because there were errors in one or more
> > mode
> > > blog.entry: Accessor for field 'userid' clashes with related field
> > 'writer.
> > > _set'. Add a related_name argument to the definition for 'userid'.
> > > blog.entry: Accessor for field 'EditUserid' clashes with related field
> > 'wri
> > > ntry_set'. Add a related_name argument to the definition for
> 'EditUserid'.
> > >
> >
> > ÎÒ¸ÃÈçºÎ£¿¶àл
>
>
> ÆäΪÄãÓÐÁ½¸ö×ֶζ¼ÊÇÒýÓÃÁËwriter£¬ËùÒÔdjango»á±¨´í£¬ÆäÖÐÒ»¸öÐèҪʹÓÃrelated_nameÀ´¶¨ÒåÒ»¸öÃû×Ö£¬¾ÍÊÇÒ»¸ö×Ö·û´®£¬Èçrelated_name
> = 'userid'
>
> --
> I like python!
> UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
> My Blog: http://www.donews.net/limodou
> _______________________________________________
> 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




-- 
ÎÒ×ßµ½Ò»¸öÄ°ÉúµÄµØ·½
¸æËß±ðÈË ÎÒҪȥÁ÷ÀË

Ŷ£¬ÎÒҪȥÁÆÉË¡­¡­

http://www.iexper.com/blog/
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070226/870862ba/attachment.htm 

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号