好的,在我的 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/