Django-celery-beat: PeriodicTask doesn't have update_or_create method....

Created on 14 Dec 2017  路  5Comments  路  Source: celery/django-celery-beat

Hello
I tried to use that method, raise an error:
TypeError: 'PeriodicTask' object is not iterable

But with CrontabSchedule
Works fine.

I think must be avalaible...

Most helpful comment

+1

pd, created = PeriodicTask.objects.update_or_create(   
    name=obj.name,         
    defaults={  
                'crontab': schedule,  
                'enabled': 0,  
                'task': 'workflow.tasks.run_workflow_by_url',  
                'kwargs': json.dumps({  
                    'product': obj.product,  
                    'url': obj.conf_address,  
                    'alert_user': alert_user,  
                    'alert_state': obj.alert_status,  
                    'run_user': obj.author.split()[0],    
                    'account': 'root',    
                    'action': 'dotask',   
                    'task': obj.name,    
                }),    
            },   
)    

error: TypeError: 'PeriodicTask' object is not iterable

All 5 comments

Having the same issue:

In [1]: pt, created = PeriodicTask.objects.get_or_create(name='test')

In [2]: pt
Out[2]: <PeriodicTask: test: {no schedule}>

In [3]: created
Out[3]: True

In [4]: pt.delete()
Out[4]: (1, {'django_celery_beat.PeriodicTask': 1})

In [5]: pt = None

In [6]: pt

In [7]: pt, created = PeriodicTask.objects.update_or_create(name='test')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-50656b1fdd26> in <module>()
----> 1 pt, created = PeriodicTask.objects.update_or_create(name='test')

TypeError: 'PeriodicTask' object is not iterable

+1

pd, created = PeriodicTask.objects.update_or_create(   
    name=obj.name,         
    defaults={  
                'crontab': schedule,  
                'enabled': 0,  
                'task': 'workflow.tasks.run_workflow_by_url',  
                'kwargs': json.dumps({  
                    'product': obj.product,  
                    'url': obj.conf_address,  
                    'alert_user': alert_user,  
                    'alert_state': obj.alert_status,  
                    'run_user': obj.author.split()[0],    
                    'account': 'root',    
                    'action': 'dotask',   
                    'task': obj.name,    
                }),    
            },   
)    

error: TypeError: 'PeriodicTask' object is not iterable

This method does exist, the issue is that PeriodicTask uses a custom QuerySet which overwrites update_or_create method (perfectly OK), but it returns only the affected instance. This is different from what the default implementation returns and it not OK.

So, this:
https://github.com/celery/django-celery-beat/blob/master/django_celery_beat/managers.py#L13
... should to be changed to return obj, created because this is what the default implementation does:
https://github.com/django/django/blob/master/django/db/models/query.py#L592

But because this code is 4 years old, there might be backwards-compatibility issues here 馃槥


In the meantime, we can just use obj = PeriodicTask.objects.update_or_create(...) without the created value. Adding to the backwards compatibility issue :cry:

This custom manager contains only one custom method, update_or_create, and it does not anything more than the regular django method. Can't we just drop this custom manager?

what other actionable things do we have that could be improved in this package. we can improve the method and release it in a major version. but the BC should be addressed carefully

Was this page helpful?
0 / 5 - 0 ratings