Ptvs: Custom Django Commands

Created on 7 Jun 2017  路  2Comments  路  Source: microsoft/PTVS

Add custom python Django commands that can easily be executed from VS (i.e. when you right click Django project -> Python -> Django Migrate / run server / etc.)

question

Most helpful comment

Glad you asked :) This is already possible, but we consider it sufficiently advanced that we don't provide UI for it.

If you look at Microsoft.PythonTools.Django.targets you'll see how we've added the existing ones. You can use these as a template and add them into your own .pyproj file, or your own .targets file and then add `

This is essentially the code you'll need (I've simplified it a bit from what I linked above - you don't need to use resource: syntax for labels, you can just put in the text).

  <PropertyGroup>
    <PythonCommands>MyCommand;$(PythonCommands)</PythonCommands>
  </PropertyGroup>

  <Target Name="MyCommand"
          Label="My Django Command"
          Returns="@(Commands)">
    <CreatePythonCommandItem Target="$(StartupPathOrManagePy)"
                             TargetType="script"
                             Arguments="check"
                             WorkingDirectory="$(WorkingDirectory)"
                             Environment="DJANGO_SETTINGS_MODULE=$(DjangoSettingsModule)"
                             ExecuteIn="Repl:My Django REPL Title">
      <Output TaskParameter="Command" ItemName="Commands" />
    </CreatePythonCommandItem>
  </Target>

If you have ideas for commands that would be useful for everyone, feel free to add them to the file I linked above and send a pull request. We can handle adding resource strings if you don't want to. The "documentation" for this feature is in Microsoft.PythonTools.targets

All 2 comments

Glad you asked :) This is already possible, but we consider it sufficiently advanced that we don't provide UI for it.

If you look at Microsoft.PythonTools.Django.targets you'll see how we've added the existing ones. You can use these as a template and add them into your own .pyproj file, or your own .targets file and then add `

This is essentially the code you'll need (I've simplified it a bit from what I linked above - you don't need to use resource: syntax for labels, you can just put in the text).

  <PropertyGroup>
    <PythonCommands>MyCommand;$(PythonCommands)</PythonCommands>
  </PropertyGroup>

  <Target Name="MyCommand"
          Label="My Django Command"
          Returns="@(Commands)">
    <CreatePythonCommandItem Target="$(StartupPathOrManagePy)"
                             TargetType="script"
                             Arguments="check"
                             WorkingDirectory="$(WorkingDirectory)"
                             Environment="DJANGO_SETTINGS_MODULE=$(DjangoSettingsModule)"
                             ExecuteIn="Repl:My Django REPL Title">
      <Output TaskParameter="Command" ItemName="Commands" />
    </CreatePythonCommandItem>
  </Target>

If you have ideas for commands that would be useful for everyone, feel free to add them to the file I linked above and send a pull request. We can handle adding resource strings if you don't want to. The "documentation" for this feature is in Microsoft.PythonTools.targets

Thanks so much! Super helpful :)

For Django, I use a test DB for integration testing - there are 2 cmds I use:

  • Create a test fixture (export my current DB which is in a specific state): manage.py dumpdata > db.json
  • Run the the server using test fixture: manage.py testserver db.json

Each time you start the test server, the DB is in the exact same state, i.e. any changes you make are discarded when you stop the server.

If you think these will be useful I'll send a pull request

Was this page helpful?
0 / 5 - 0 ratings