About Form Wizards in Administration

You can create a custom form to send emails, tweet, or do some external action in administration by creating an unmanaged model with a custom save() method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class NewEmailMessage(models.Model):
    sender = models.EmailField(_("Sender"))
    recipient = models.EmailField(_("Recipient"))
    subject = models.CharField(_("Subject"), max_length=255)
    body = models.TextField(_("Body"), blank=True)

    class Meta:
        managed = False

    def save(self, *args, **kwargs):
        send_mail(
            subject=self.subject, message=self.body,
            from_email=self.sender, recipient_list=[self.recipient],
        )

Then in the administration of this model, disable the actions to view, edit, and delete:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@admin.register(NewEmailMessage)
class NewEmailMessageAdmin(admin.ModelAdmin):
    fields = ("sender", "recipient", "subject", "body")

    def has_add_permission(self, request):
        return True

    def has_change_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def has_view_permission(self, request, obj=None):
        return False

Tips and Tricks Programming Architecture Development Django 5.x Django 4.2 Django 3.2