The Django administration site comes with a couple of default entries, depending on which apps and middleware is installed.
Usually there is at least "Users" and "Groups", and if the quite common "allauth" is installed, then there is also "Site", "SocialApp", "SocialAccount", and "SocialToken". They are not always necessary, especially when no Social Login is used. Or why have the "Site" administration when only Site=1 is used?
With a few tricks these can be removed from the admin menu.
Continue reading "Django: Remove default entries from admin menu"
The Django Web Framework makes it quite easy to add new referenced objects in the admin menu.
Let's say the model has two foreign keys in it:
class TeamMember(models.Model):
team = models.ForeignKey(Team, on_delete=models.CASCADE, verbose_name=_("Team"))
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, verbose_name=_("User"))
And the admin form:
class TeamMemberForm(forms.ModelForm):
class Meta:
model = TeamMember
class CustomTeamMemberAdmin(admin.ModelAdmin):
form = TeamMemberForm
add_form = TeamMemberForm
model = TeamMember
fieldsets = [
(None, {'fields': ['team', 'user']}),
]
admin.site.register(TeamMember, CustomTeamMemberAdmin)
Then Django will show small a small green "
" sign next to the fields:

Continue reading "Django: disable inline option to add new referenced objects"
A simple task, or so I thought: in a Jinja template keep track of the number of items in a loop. And then use that count afterwards.
Disclaimer: the number of items is not equal the number of times the loop runs, so I can't use the loop variables.
Turns out that Jinja has other opinions, and variables inside a loop are all local. When a variable is changed inside the loop, the scoop of the variable stays local, and once the loop ends the original value of the variable is restored. That's even true for variables which are created outside the loop.
Continue reading "Use namespace as global variable in Ansible Jinja templates"
In one of my Ansible Playbooks I'm updating Let's Encrypt certificates. Because the Playbook is rather long, I want to make sure that Ansible is not spending cycles on a certificate which is not about to expire. Hence I wrote a small filter plugin, which receives the certificate path and the number of days the certificate must be still valid.
This plugin is used to filter out any "good" certificate which does not need to be updated.
Continue reading "Certificate expiration date in Ansible"