ManyToMany字段引用中的Django NameError [英] Django NameError during ManyToMany field referencing

查看:165
本文介绍了ManyToMany字段引用中的Django NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为PlayCat的表。其基本上存储有趣的活动的类别名称。比如迪斯科舞厅,游泳池等等。所以我想让这些类别被引用(ManyToMany),当用户创建一个Play竞技场时,他/她可以选择它所属的类别。



播放: p>

  class Play(models.Model):
shopname = models.CharField(max_length = 100)
desc = models.CharField(max_length = 500,blank = True,null = True)
address = models.CharField(max_length = 300)
category = models.ManyToManyField(PlayCat)
url = models。 URLField(blank = True,null = True)
email = models.EmailField(blank = True,null = True)
slug = models.SlugField(blank = True,null = True)
phone = models.CharField(max_length = 40,blank = True,null = True)

def __unicode __(self):
return self.shopname

在同一个models.py中,我有PlayCat

 code> class PlayCat(models.Model):
cat = models.CharField(max_length = 45)
def __unicode __(self):
return self.cat

我还填充了PlayCat数据库,但是当我添加M2M引用错误消息时,终端中会出现

  _import __(name)
文件/home/saud/Downloads/Django-1.2.3/ms/ms/ms/sale/models.py,第104行在< module>
class Play(models.Model):
文件/home/saud/Downloads/Django-1.2.3/ms/ms/ms/sale/models.py,第108行,Play
category = models.ManyToManyField(PlayCat)
NameError:name'PlayCat'未定义

我不明白为什么NameError和Wat我没有定义。请帮忙。谢谢。

解决方案

我想是因为你有一个模型Play之后定义的PlayCat模型。所以它无法解决它。



您可以在播放前放置模型PlayCat或使用字符串进行参考

  category = models.ManyToManyField('PlayCat')


I am having a table named PlayCat. which basically stores al the category names of playful activities. such as disco,pool n stuff. So i want these categories to b referenced(ManyToMany) when a user creates a Play arena where he/she can select al the categories it belongs to.

Play :

class Play(models.Model):
  shopname=models.CharField(max_length=100)
  desc=models.CharField(max_length=500,blank=True, null=True)
  address=models.CharField(max_length=300)
  category = models.ManyToManyField(PlayCat)
  url=models.URLField(blank=True, null=True)
  email=models.EmailField(blank=True, null=True)
  slug=models.SlugField(blank=True, null=True)
  phone=models.CharField(max_length=40,blank=True, null=True)

  def __unicode__(self):
    return self.shopname

and in the same models.py I have 'PlayCat'

class PlayCat(models.Model):
  cat = models.CharField(max_length=45)
  def __unicode__(self):
    return self.cat

I have also populated the PlayCat database but when i add M2M referencing error message appears in the terminal saying

_import__(name)
  File "/home/saud/Downloads/Django-1.2.3/ms/ms/ms/sale/models.py", line 104, in <module>
    class Play(models.Model):
  File "/home/saud/Downloads/Django-1.2.3/ms/ms/ms/sale/models.py", line 108, in Play
    category = models.ManyToManyField(PlayCat)
NameError: name 'PlayCat' is not defined

I dont understand why is the NameError and wat i have not defined. Please help. Thank You.

解决方案

I guess it's because you have a model PlayCat defined after a model Play. So it can't resolve it.

You can either put model PlayCat before Play or use a string for reference

category = models.ManyToManyField('PlayCat')

这篇关于ManyToMany字段引用中的Django NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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