4

I have a model:

class Object(Object1): name = models.CharField(max_length=255, blank=True) description = models.TextField(blank=True) date_updated = models.DateTimeField(blank=True, null=True) 

I want to track if there is anything inserted into the date_updated field, then create another object in another model, without overriding the save method.

For example:

if date_updated: MyModel.objects.create(type="D", user=request.user) 

Although I have tried this, but still no success.

6
  • "without overriding the save method" - why do you have this requirement? Commented Apr 13, 2016 at 13:43
  • Could you also add the code which you have tried? Commented Apr 13, 2016 at 13:44
  • 1
    have you considered using the post_save signal ? Commented Apr 13, 2016 at 13:46
  • it is actually not a requirement, I just tried to override the save method, i also tried signals, and all the possible solutions in the link attached above, but no success. Commented Apr 13, 2016 at 13:46
  • Django signals like pre_save or post_save could be useful: docs.djangoproject.com/en/1.9/ref/signals Commented Apr 13, 2016 at 13:47

2 Answers 2

9

You can use tracker field from django-model-utils.

Add a tracker to the model:

class Object(Model): name = models.CharField(max_length=255, blank=True) description = models.TextField(blank=True) date_updated = models.DateTimeField(blank=True, null=True) tracker = FieldTracker() 

You can check in save() or in other places where you usually update the model:

if object.tracker.has_changed('date_updated'): create_new_object(data) 
Sign up to request clarification or add additional context in comments.

Comments

1

Sorry, just noticed that you are referencing request.user. So this will NOT work for you. Since you need the particular request object, its probably best done in the view that the request object is referring to.

With pre_save you could compare the current instance's property value with the one currently in the database.

@receiver(pre_save, sender=MyModel) def check_date(sender, instance=None, created=False, **kwargs): changed = created or instance.date_updated != MyModel.objects.get(pk=instance.pk).date_updated if changed: MyModel.objects.create(type="D", user=request.user) 

Didn't test it, though.

3 Comments

can you please give an example of how to do this in my case? because i tried a lot of things so far.
can I get the user from kwargs here?
pre_save signal has not created arg, it is in post_save signal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.