bigzap
bigzap

Reputation: 3

how to access django models with script outside of the project

I have two projects. The new one bases on Django and the old one based on PHP. Both are alive. I want to synchronize all info between these projects. I have a python script which parse all info from the PHP project but I can't load it to the Django project.

The only way I found to solve the problem is to connect to sqllite DB of the django project from my python script. But I feel that this solution is not the best&

Is there a way to write smth like this in my python script:

from .models import MyModel
for (f1, f2, f3) in parse_results:
    result = MyModel.objects.create(field1=f1, field2=f2, field3=f3)

For now when i try to import MyModel i see ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package

Upvotes: 0

Views: 250

Answers (1)

Claudio Catterina
Claudio Catterina

Reputation: 337

You can write your script code in a Django custom command, in this way you have access to your Django models.

Upvotes: 1

Related Questions