在django 1.8
models.py
class hotel(models.Model):
hotel_name = models.CharField(max_length=20)
class Sell(models.Model):
hotel = models.ForeignKey("hotel")
sell_text = models.CharField(vmax_length=20)
class Selled(models.Model):
buy_choices = [(data.id, data.sell_text) for data in Sell.objects.filter(Hotel_id=2)]
city = models.IntegerField(choices=city_choices, default=1)命令./manage.py e.py runserver
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.为什么我不能用Hotel_id来过滤
外卖Hotel_id将运行
发布于 2015-09-07 10:04:37
在django成功建立数据库之前,您正在尝试执行数据库查询,因此django告诉您“模型尚未加载”。
看起来,您似乎希望通过查询提供模型字段的动态选择,而这是无法执行加载时间的。而且,看起来您需要的是ForeignKey,而不是IntegerField。
你应该看看limit_choices_to。
class Selled(models.Model):
city = models.ForeignKey('Sell', limit_choices_to={'id': 2})发布于 2015-09-07 09:49:12
在开发服务器中运行应用程序之前,必须将项目连接到现有数据库(或使用SQLite创建一个快速运行的数据库)。
设置正确使用DB后,必须运行以下命令。
python manage.py migrate这将在数据库中创建必要的表以开始工作。您可以在这里查看与DBs相关的所有必要信息。https://docs.djangoproject.com/en/1.8/ref/databases/
https://stackoverflow.com/questions/32435603
复制相似问题