Ask Your Question

Wing scripts in a django project

asked 2022-01-08 19:43:40 -0500

bai's avatar

updated 2022-01-08 19:44:24 -0500

I want to write a script to help me do some automation jobs in a django project. There is a model named "Post", I can use the code below to get all the fields:

from . import models


How can I use the code in a wing script? I wish I can access the django repl to get the data and use them back in the script. For example:

s = make_useful_string(models.Post._meta.fields)


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2022-01-10 09:45:38 -0500

You may be able to import models by adding directories to sys.path (probably the parent directory of the package models is in and the paths that Django needs) and then importing the module. The code would need work with the version of Python that Wing uses internally (Python 3.9 in Wing 8) and you'd need to restart Wing when your code changes.

edit flag offensive delete link more


Thank you for the idea!

bai's avatar bai  ( 2022-01-10 18:40:05 -0500 )edit

answered 2022-01-10 14:59:28 -0500

Another possibly cleaner way is to write your script as a stand-alone tool that can be run on the command line and outputs the results you need. Then use the AsyncExecute* or ExecuteCommandLine methods on the CAPIApplication in the scripting API to call it and read its output. That way you don't have to match Python version, etc.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2022-01-08 19:43:40 -0500

Seen: 356 times

Last updated: Jan 12 '22