python - 为什么我不断收到 NameError : name 'PS' is not defined

标签 python django class django-models

好的,在我的 models.py 文件中,我刚刚创建了这个:

class PL(models.Model):
  created = models.DateTimeField(default=timezone.now)
  owner = models.ForeignKey(User, related_name='PL')
  text = models.CharField(max_length=2000, blank=True)
  rating = models.IntegerField(default=0)
  pal = models.ManyToManyField(PS, blank=True, null=True)
  class Meta:
    verbose_name = "PL text"
  def __unicode__(self):
    return self.user

class PS(models.Model):
  Original = models.ForeignKey(PL, related_name='OPL', blank=True)
  rating = models.IntegerField(default=0)
  word = models.CharField(max_length=50, blank=True)

  def __unicode__(self):
    return "Word: %s" % (self.word)

但我不断收到:NameError: name 'PS' is not defined

为什么会这样?

最佳答案

正如 mgilson 所说,它从上到下。但是 Django 有办法通过这样做来克服它 -

pal = models.ManyToManyField('PS', blank=True, null=True)

Django 文档在 ForeignKey 下对其进行了描述。

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself.

您可以 read more here .

关于python - 为什么我不断收到 NameError : name 'PS' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14938798/

相关文章:

python - 如何在 Python 3 CGI 中打印 unicode 字符?

python - Pandas 系列 – 最后 x 行中的最低值

Django:连接两个表

python - 由于无法进行就绪检查,因此Google App Engine部署失败

Java:静态字段在内存中的什么位置?

java - 为什么 Java 不编译这段代码?

c# - 一行中的类初始化语法

python - 从 HSV 获取亮度值 - Python

Python:调用函数真的很慢?

python - Django : cannot unpack non-iterable int object