python - How to import model in Django -


i want execute python file want check if there new rows in csv file. if there new rows, want add them database.

the project tree follows:

enter image description here

thus, file want execute check_relations.py inside relations folder.

check_relations.py follows:

from master.models import traxiorelations  open('autoi_relaties.csv', 'rb') tr:     traxio_relations = tr.readlines()  line in traxio_relations:     number = line.split(';')[0]     number_exists = traxiorelations.objects.filter(number=number)     print(number_exists) 

the model traxiorelations inside models.py in master folder.

when run python check_relations.py error

traceback (most recent call last):   file "check_relations.py", line 3, in <module>     master.models import traxiorelations importerror: no module named master.models 

what doing wrong? how can import model inside check_relations.py file?

i think usable way create commands

for example in master catalog:

master /     management /         __init__.py         commands /             __init__.py             check_relations.py 

in check_relations.py

from django.core.management.base import basecommand master.models import traxiorelations  class command(basecommand):     = 'check_relations file data'      def handle(self, *args, **options):          open('autoi_relaties.csv', 'rb') tr:             traxio_relations = tr.readlines()          line in traxio_relations:             number = line.split(';')[0]             number_exists = traxiorelations.objects.filter(number=number)             print(number_exists) 

! don't forget change path file autoi_relaties.csv or put new dir

and can run in shell:

./manage.py check_relations 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -