60👍
Try this
import sys, os, django
sys.path.append("/path/to/store") #here store is root folder(means parent).
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "store.settings")
django.setup()
from store_app.models import MyModel
This script you can use anywhere in your system.
15👍
This sounds like a great use case for Django Management commands. which has the added bonus you can run it scheduled from cron, direct from the commandline, or call from inside django. This gives the script full access to the same settings and environment variables as your main project.
If you move this into an appropriate directory – using store here as an example, not a suggestion, it should work.
store
management
__init__.py
commands
__init__.py
otherscript.py
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
- [Django]-__init__() got an unexpected keyword argument 'user'
- [Django]-How to receive json data using HTTP POST request in Django 1.6?
3👍
Django standalone script.
- Place it in the same directory as manage.py file
- Rename PROJECT_NAME to your project’s name
import os
PROJECT_NAME = 'ENTER YOUR PROJECT NAME HERE'
def main():
# import statemts
# from app.models import Author,Category,Book
# code logic - anything you want
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings' % PROJECT_NAME)
import django
django.setup()
main()
- [Django]-'EntryPoints' object has no attribute 'get' – Digital ocean
- [Django]-Django urlsafe base64 decoding with decryption
- [Django]-Separation of business logic and data access in django
2👍
I found the django-extensions
package to be most useful. You can find it here
After installation and adding it to the installed apps, all you need to do is to create a directory called scripts
, create a file and run it by python manage.py runscript file_name
Just remember that the file must have an entry point. That entry point is a method called run
which has to be present and will run once you run python manage.py runscript file_name
in the terminal
- [Django]-Querying django migrations table
- [Django]-How to create a custom decorator in Django?
- [Django]-Unresolved reference: 'django' error in PyCharm
1👍
try to import from store_app.models
– as the surrounding store
folder is not a python module and should not be used when importing.
import django
from django.conf import settings
settings.configure(DEBUG=True)
django.setup()
from store_app.models import MyModel
update: i just noticed that you put that script next to your project folder – you should move it inside for this to work.
- [Django]-Django i18n: Make sure you have GNU gettext tools
- [Django]-How can my django model DateField add 30 days to the provided value?
- [Django]-What is the absolute path of BASE DIR?
0👍
import django
from django.conf import settings
>>from store.store_app.apps import StoreAppConfig<<
This line can be deleted if "other_script.py" moves to the "store" folder & "settings.py" file inserts "other_script" name in "instaled_apps".
Don’t add "other_cript" to "instaled_apps" & change it to:
>>from store import settings
settings.configure(settings, DEBUG=True)
django.setup()<<
and in the end I think:
>>from store.store_app.models import MyModel<<
change this to:
>>from store_app.models import MyModel<<
- [Django]-How to generate temporary file in django and then destroy
- [Django]-Django's ManyToMany Relationship with Additional Fields
- [Django]-ForeignKey to abstract class (generic relations)