celery / django-celery-beat

Celery Periodic Tasks backed by the Django ORM
Other
1.69k stars 428 forks source link

Cannot programmatically set a schedule #611

Open paxw-panevo opened 1 year ago

paxw-panevo commented 1 year ago

Summary:

A schedule cannot be programmatically set without including the schedule in the form and not marking it as read-only. I have to include the crontab (for example) field and not mark it as a read-only although in the backend, the crontab field is completely ignored and set programmatically. This is not good UX,

Ideally, the crontab field is either marked as read-only or completely not displayed on the form. The user still is able to create a task successfully since the crontab field is programmatically set in the backend.

Exact steps to reproduce the issue:

  1. Create a ModelAdmin where crontab is read-only
  2. Create a ModelForm and override clean() to programmatically set the crontab
  3. In the django-admin UI, create a task

Expected: The user is able to create a task with a crontab programmatically set by the backend. Actual: The user receives an error One of clocked, interval, crontab, or solar must be set.'

Detailed information and proposed change

django excludes fields that are not on the form or read-only fields when validating uniqueness of model instances, https://github.com/django/django/blob/d44ee518c4c110af25bebdbedbbf9fba04d197aa/django/forms/models.py#L380

but django_celery_beat checks that a schedule must exist in the instance during validate_unique() https://github.com/celery/django-celery-beat/blob/975f8ea2a9d0ff4f2809a3c34e43ad3a848f148d/django_celery_beat/models.py#L550-L574

A potential solution to this issue then is to remove the validation of a schedule existing in validate_unique() and move it to a more appropriate method like clean() -- I don't think checking a schedule's existence is relevant to whether the task is unique? If this sounds reasonable, I can create a PR.

cfvphxzx762 commented 1 year ago

Hi, my friend, have you solved this problem? Is this feedback a problem of celery itself? I've looked for many ways, but I can't fix it. :(

auvipy commented 1 year ago

you can come with a PR with failing tests