CNK's Blog

Django Commands


Someone on the PyDev Slack channel was having trouble getting emails from the Django admin system and another member mentioned there is a sendtestemail command that can help one debug your email settings. That made me wonder what else is available that I didn’t know about.


The Django settings object is a bit odd in that you can’t do print(settings.__dict__) to figure out what is available - and even if you know (or guess) the name of a setting, how do you know if the value is the default or something you have overridden in your app? There is a command for that! The most useful version is python diffsettings --output unified. That gives you all the settings - with the overridden versions in red.


I have used the default version to check to see if I have applied all the existing migrations in my test and prod environments - that’s the python showmigrations --list version. But there is also a python showmigrations --plan version. That will show you the order in which Django will apply migrations.


If you run python inspectdb against an existing database, it will generate the Django models that would have created those tables (and indexes and constraints). This command is for projects that must use a legacy database so all of the models are created with a Meta class with managed = False.


If your site has had a bunch of changes that you want Google to recrawl, you can use this command to submit your sitemap: python ping_google. If you have the Django sitemaps app installed and a url configured for your sitemap, this command will figure out what the url should be.