Squallcx
Squallcx

Reputation: 125

How to fix Models aren 't loaded yet

in 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)

command ./manage.py runserver

   django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

why i can't use Hotel_id for filter

takeout the Hotel_id Will be running

Upvotes: 1

Views: 571

Answers (2)

meshy
meshy

Reputation: 9076

You are trying to do a database query before django has managed to set up the database, so django tells you that the "Models aren't loaded yet".

It looks as though you want to give a model field dynamic choices with a query, which you cannot do a load time. What's more, it looks like you need a ForeignKey, not an IntegerField.

You should look at limit_choices_to.

class Selled(models.Model):
    city = models.ForeignKey('Sell', limit_choices_to={'id': 2})

Upvotes: 2

acostela
acostela

Reputation: 2727

Before you can run your application in the development server you must connect your project to an existing database (or create one onfly with SQLite).

After settings are correct to use a DB you must run the following command.

python manage.py migrate

This will create necessary tables inside your DB to start working. You can check all necessary information in relation with DBs here. https://docs.djangoproject.com/en/1.8/ref/databases/

Upvotes: -1

Related Questions