Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 2.0 Access Models (CREATE/REMOVE/FILTER) Standalone [without manage.py shell]

I have a Django project and I wanted to generate some objects (from the models)

What I'm trying to get at : Standalone Python Script to create bunch of objects and/or filter,delete.

after importing the model with from apps.base.models import MyModel and setting up the configuration as the previous StackOverflow Questions suggested I was not able to run the script.

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myProject.settings")
import django

django.setup()
from apps.base.models import MyModel

Please note that this is on Django version 2.0.6 [Django 2.0+].

Correct settings have been used, (i.e. myProject.settings)

  • After properly configuring everything else I get the following error:

    • RuntimeError: Model class apps.base.models.MyModel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Settings:

  • base.py : https://pastebin.com/MNcitE4U

  • development.py : https://pastebin.com/JAHqxwRM

The full GitHub link is here: https://github.com/teratzu/Razred-Management

like image 706
innicoder Avatar asked Jun 28 '18 17:06

innicoder


People also ask

What is the difference between the django shell and a normal interactive python shell?

Directly typing python, just starts the interpreter. Using manage.py within a django project sets the environment so that you can interact with your project objects in the shell.

What does the django command manage py shell do?

When you run python manage.py shell you run a python (or IPython) interpreter but inside it load all your Django project configurations so you can execute commands against the database or any other resources that are available in your Django project.

How do I import a django model into a python script?

Use the following and you will be able to access your model:import os os. environ. setdefault("DJANGO_SETTINGS_MODULE", "your_project_name. settings") # your imports, e.g. Django models from your_project_name.

What is admin py in django?

django-admin.py is Django's command-line utility for administrative tasks. This document outlines all it can do. In addition, manage.py is automatically created in each Django project.


1 Answers

I see two issues in the current project structure.

Missing AppConfig

As the other answer suggested correctly, you need to provide an AppConfig subclass to register your application; see Configuring applications section in Django docs.

However, having an apps package for namespace declarations is totally fine, especially if you plan to have multiple apps; in fact, this is what Django does itself, bundling all batteries apps under the django.contrib namespace. At my work, we also use common namespaces to organize Django apps: mycompany.internal.someapp, mycompany.customers.otherapp etc.

So you don't need to move the files. Just reference the apps.base correctly: create a file apps/base/apps.py with the content:

# apps/base/apps.py
from django.apps import AppConfig


class MyBaseAppConfig(AppConfig):
    name = 'apps.base'

Now reference the created config class of the app apps.base in the settings:

# DzenanElvir/settings/base.py

INSTALLED_APPS = [
    ...
    # Local apps
    'apps.base.apps.MyBaseAppConfig',
]

If apps should be a package, treat it like one

So you declared the apps package (by placing an__init__.py file in the apps dir). However, you never use the package, instead fiddling the sys.path by adding the apps dir to it. If the app should be importable under apps.base, remove the path fiddling line in the DzenanElvir/settings.base.py:

# remove this line:
path.append(os.path.join(PROJECT_ROOT, "apps"))

and use the right imports throughout the app, replacing:

  • from .models with from apps.base.models
  • from .forms with from apps.base.forms
  • from .pregledView with from apps.base.pregledView
  • from .postsView with from apps.base.postsView

Also, don't forget to replace

urlpatterns = [
    url(r'', include('base.urls')),
    ...
]

with

urlpatterns = [
    url(r'', include('apps.base.urls')),
    ...
]

in DzenanElvir/urls.py and you should be good to go.

like image 80
hoefling Avatar answered Sep 30 '22 18:09

hoefling