Django models.py循环外键 [英] Django models.py Circular Foreign Key

查看:187
本文介绍了Django models.py循环外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django应用程序,它基本上只是一张相册。现在我有两个模型:图像相册。除了别的以外,每个相册有一个外键,一个 Image 成为它的缩略图,每个图像具有它所属的相册的外键。但是,当我尝试使用 manage.py syncdb manage.py sqlall 我收到的错误说,在第一类使用时,没有定义在models.py中没有定义的类



models.py(abridged):

  from django .db导入模型
import os

class Album(models.Model):
thumb = models.ForeignKey(Image,null = True,blank = True)

class Image(models.Model):
image = models.ImageField(upload_to ='t_pics / images')
thumb = models.ImageField(upload_to ='t_pics / images / thumbs')
album = models.ForeignKey(Album)

$ c> manage.py sqlall appname :

  [...] 
文件/路径/到/文件/应用程序的名字/ models.py,第4行,在?
class Album(models.Model):
文件/path/to/file/appname/models.py,第5行,专辑
thumb = models.ForeignKey(Image,null = True,blank = True)
NameError:name'Image'未定义

我当我在models.py中切换类的顺序时,得到相同的错误,除了它说'Album'undefined 而不是'Image'undefined 我还尝试评论第一类的依赖关系,然后在其他所有内容成功导入后取消注释,但没有帮助。我该如何去做这项工作?我不愿意做一个完整的三等课程 Thumb ,因为它将有很多相同的代码 Image I '也很确定我可以手动将外键添加到数据库,但我希望这是干净而不是黑客。

解决方案

你实际上没有循环引用;问题是,在您定义相册时,您尚未定义图像。您可以使用字符串来修复:

  class Album(models.model):
thumb = models。 ForeignKey('Image',null = True,blank = True)

然而,在这种情况下,你可能需要使用 OneToOneField 而不是外键。 (请注意,您仍然必须使用该字符串的技巧)。


I have a django app which basically is just a photo album. Right now I have two models: Image and Album. Among other things, each Album has a foreign key to an Image to be its thumbnail and each Image has a foreign key to the Album it belongs in. However, when I try to use manage.py syncdb or manage.py sqlall I get errors saying the class not defined first in models.py isn't defined when it is used in the first class defined.

models.py (abridged):

from django.db import models
import os

class Album(models.Model):
    thumb = models.ForeignKey(Image, null=True, blank=True)

class Image(models.Model):
    image = models.ImageField(upload_to='t_pics/images')
    thumb = models.ImageField(upload_to='t_pics/images/thumbs')
    album = models.ForeignKey(Album)

Error I get when I do manage.py sqlall appname:

[...]
 File "/path/to/file/appname/models.py", line 4, in ?
    class Album(models.Model):
  File "/path/to/file/appname/models.py", line 5, in Album
    thumb = models.ForeignKey(Image, null=True, blank=True)
NameError: name 'Image' is not defined

I get the same error when I switch the order of the classes in models.py except it says 'Album' undefined instead of 'Image' undefined I also tried commenting the dependancy in the first class then uncommenting after everything else was successfully imported but that didn't help. How should I go about making this work? I'm reluctant to make an entire third class Thumb because it will have a lot of the same code as Image I'm also pretty sure I could manually add the foreign key to the database but I want this to be clean and not hackish.

解决方案

You don't actually have a circular reference; the issue is that, at the time you define Album, you haven't defined Image yet. You can fix that by using a string instead:

class Album(models.model):
  thumb = models.ForeignKey('Image', null=True, blank=True)

However, in this case, you might want to use a OneToOneField instead of a foreign key. (Note that you'll still have to use the trick with the string, though).

这篇关于Django models.py循环外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆