Django save post via celery
I have a particular use case where i write text for a blog post in admin dashboard. Then i want to generate the embeddings of post.
I am doing this in admin.py
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'created_at')
readonly_fields = ('embeddings_calculated','emebddings_updated_at')
def save_model(self, request, obj, form, change):
if not obj.meta_title:
obj.meta_title = obj.title
obj.meta_description = obj.content[:160]
super().save_model(request, obj, form, change)
print("django post id",obj.id)
post = get_object_or_404(Post, id=obj.id)
print("django post title",post)
if not change: # if new post not an edit post
task_id = perform_task.delay(obj.id)
result = AsyncResult(str(task_id), app=app)
print("AsyncResultx", result)
print("AsyncResultx_state", result.state)
here is my celery task
@shared_task()
def perform_task(post_id):
print("celery post id",post_id)
post = get_object_or_404(Post, id=post_id)
print("celery post title",post)
I am getting this error
django.http.response.Http404: No Post matches the given query.
Though the print statement such as django post id and django post title show me correct result. But the issue lies in celery. I cant see celery post title in console.
I aslo tried
task_id = transaction.on_commit(lambda: perform_task.delay(obj.id))
But it did not work as well. Without celery every thing work well, but i want to use celery.