[Answer]-Django Rest Framework: best way to populate field manually

1๐Ÿ‘

Iโ€™m not a big fan of class based views, I prefer fat models and thin views.

I would opt for an post_save signal on Request and check the created param.

https://docs.djangoproject.com/en/dev/ref/signals/#post-save

Request(...):
    @classmethod
    def post_save(cls, sender, instance, created, raw, **kwargs):
        if raw:
            return
        if created:
            pass  # do your on create stuff here

post_save.connect(Request.post_save, sender=Request, dispatch_uid="uniqueid")  # use weak=False if you use a local function

Also note that instance.save() triggers the save signals again, so you might consider to just use .update() if you want to avoid all your signals to be processed again (or even worse, create a signal loop ๐Ÿ˜‰ )

๐Ÿ‘คACimander

Leave a comment