0

I am trying to populate models using populate script. I have set up models,and did migrations. I have tried every solution that I found on internet so far, but it did not work. It gives : "RuntimeError: Model class first_app.models.Topic doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS."

manage.py

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_project.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

populate_first_app.py

import os
from django.conf import settings
settings.configure()
os.environ.setdefault("DJANGO _SETTINGS_MODULE",'first_project.settings')
import django

django.setup()
import random
from first_app.models import AccessRecord, Webpage,Topic
from faker import Faker

fake_object=Faker();
topics=['Search,''Social','Marketplace','News','Games']
genders=['Male','Female']

def add_topic():
    t=Topic.objects.get_or_create(top_name=random.choice(topics))[0]
    t.save()  
    return t;


def populate(N=5):
    for entry in range(N):
        fake_topic=add_topic().top_name;
        fake_name=fake_object.company();
        fake_url=fake_object.url();
        webpage=Webpage.objects.get_or_create(topic=fake_topic,name=fake_name,url=fake_url)[0]
      

if __name__=='__main__':
    print('populating script');
    populate(20);
    print('populated')












models.py

from django.db import models

class Topic(models.Model): 
    top_name =models.CharField(max_length=264,unique=True);
    def __str__(self) :
        return self.top_name;


class Webpage(models.Model):
    topic=models.ForeignKey(Topic,on_delete=models.CASCADE)
    name=models.CharField(max_length=264,unique=True)
    url=models.URLField(unique=True)

    def __str__(self) :
        return self.name;

class AccessRecord (models.Model):
    name=models.ForeignKey(Webpage,on_delete=models.CASCADE);
    date=models.DateField()
    def __str__(self) :
       
     return str(self.date)

# Create your models here.

INSTALLED_APPS


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'first_app'
]

apps.py

from django.apps import AppConfig

class FirstAppConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'first_app'


folder structure enter image description here

Parviz Pirizade
  • 193
  • 2
  • 12
  • Please add your project file structure – Mohamed Hamza Mar 13 '22 at 09:00
  • It looks like you defined your app in `firts_app` (so swapped the `s` and the `t`). – Willem Van Onsem Mar 13 '22 at 09:03
  • it's first_app, where it is firts app? – Parviz Pirizade Mar 13 '22 at 09:31
  • `os.environ.setdefault("DJANGO _SETTINGS_MODULE",'first_project.settings')`, has a typo, an extra space between `DJANGO` and `_SETTINGS`. But have you checked the answers at https://stackoverflow.com/questions/40206569/django-model-doesnt-declare-an-explicit-app-label. By the way, the **firts** is in your question title, though I don't think that's your error in your code. – raphael Mar 13 '22 at 14:41

0 Answers0