Django说我的模型没有定义 [英] Django says my Model is not defined

查看:123
本文介绍了Django说我的模型没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用Django开发一个项目,我试图在用户和国家之间创建一些模型之间的几个关系。当我尝试syncdb我的控制台输出名称国家没有定义。查看代码:

  class User(models.Model):
name = models.CharField(max_length = 50 ,null = False)
email = models.EmailField(max_length = 50,null = False)
password = models.CharField(max_length = 10,null = False)
country = models.ForeignKey (Country,null = False)#error here
rol = models.ForeignKey(Rol,null = False)
job = models.ManyToManyField(Job)#UserxJob
skill = models.ManyToManyField技能)#UserxSkill
plan = models.ManyToManyField(Plan)#UserxPlan
image = models.ForeignKey(Image)
description = models.TextField(max_length = 300)
website = models.URLField(max_length = 100,null = True)

def __unicode __(self):
return self.name


class国家(模型.Model):
name = models.CharField(max_length = 50,null = False)

def __unicode __(self):
return self.name

你能帮我一下吗?

解决方案

国家/地区的类定义移动到文件用户 p>



User 模型中,更新属性 country to:

  country = models.ForeignKey('Country' null = False)

此文档可以是在这里找到


So I´m developing a project using Django and I´m trying to create several relationships between some models such as User and Country. When I try to syncdb my console outputs " Name Country is not defined". Check out the code:

class User(models.Model):
 name = models.CharField(max_length=50,null=False)
 email = models.EmailField(max_length=50,null=False)
 password = models.CharField(max_length=10,null=False)
 country = models.ForeignKey(Country,null=False) #error here
 rol = models.ForeignKey(Rol,null=False)
 job = models.ManyToManyField(Job) #UserxJob
 skill = models.ManyToManyField(Skill) #UserxSkill
 plan = models.ManyToManyField(Plan) #UserxPlan
 image = models.ForeignKey(Image)
 description = models.TextField(max_length=300)
 website = models.URLField(max_length=100,null=True)

 def __unicode__(self):
    return  self.name


class Country(models.Model):
 name = models.CharField(max_length=50,null=False)

 def __unicode__(self):
    return self.name

Could you please help me out with this one?

解决方案

Either move the class definition of Country on above User in the file

OR

In the User model, update the attribute country to:

country = models.ForeignKey('Country',null=False) 

Documentation on this can be found here

这篇关于Django说我的模型没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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