django-rq icon indicating copy to clipboard operation
django-rq copied to clipboard

Safety of model instances with related objects as initial parameter to jobs

Open ta2-1 opened this issue 9 years ago • 2 comments

There are three models and meta-job function:

class ModelA(models.Model):
    child = models.ForeignKey(ModelB)

class ModelB(models.Model):
    child = models.ForeignKey(ModelC)

class ModelC(models.Model):
    name = models.CharField()
    child = None

def func(obj):
    if obj.child is not None:
        queue.enqueue(func, obj.child)

Instance of ModelA is passed as parameter to a RQ job. Related objects of ModelB, ModelC will be necessary after corresponding RQ job is executed.

I can fetch initial object without related.

obj = ModelA().objects.get()
...
queue.enqueue(func, obj)

Or I can fetch object with related objects before enqueueing a first job because I see that RQ keeps cached related objects in a job redis hash key.

obj = ModelA().objects.select_related('b__c').get()
...
queue.enqueue(func, obj)

There are less queries to my database when I use select_related but I wonder if it is right and safe. How will Django handle all these cached related objects?

ta2-1 avatar Jan 28 '16 16:01 ta2-1

Hey this is a really good question! It's something that every Django developer working with periodic tasks will have to deal with at some point ---> the desire to pass objects around and do work on them in periodic tasks... without using an ID field or other PK type field. Just helps write cleaner code, I think.

Going to test this out in my application and will report back.

manickbhan avatar Oct 09 '20 04:10 manickbhan

Worked perfectly! This is good!

manickbhan avatar Oct 09 '20 05:10 manickbhan