0

I have two models:

class CustomUser(models.Model):
    ...
    act = models.CharField(max_length=10)

class ActHistory(models.Model):
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    last_act = models.CharField(max_length=10)
    act_time = models.DateTimeField(auto_now_add=True)

I would like to create a single API endpoint /update/ with DRF that does this:

  1. If value vs existing act value in the model is the same, do nothing.
  2. If the values are different, update the CustomUser for the authenticated user with the value
  3. Then insert a record row in ActHistory with user=authenticated user, last_act =

All the docs I managed to find only support doing either the update or create actions with each API point.

Thanks in advance!

Joshua
  • 197
  • 12
  • 1
    Have you tried anything so far? Please show some effort. – JPG Jun 13 '19 at 08:02
  • @Joshua Please show some effort – anjaneyulubatta505 Jun 13 '19 at 08:22
  • you can use post signal or models method in example models save method. also you can check reference change fields in models here: https://stackoverflow.com/questions/1355150/django-when-saving-how-can-you-check-if-a-field-has-changed – flakavir Jun 13 '19 at 08:41
  • @flakavir I'm looking at the save override, does it mean I'll construct my API as an UpdateView for CustomUser which would run the comparison on the model backend and create a new record if there was a change? This would seem to solve my problem but it seems like a hacky approach. Is it proper practice to create another model from within a model's construction? – Joshua Jun 13 '19 at 10:51
  • just the models, when you save or update models from API, it will use save models method. – flakavir Jun 13 '19 at 14:38

0 Answers0