M
M
marselabdullin2020-06-11 10:49:41
Django
marselabdullin, 2020-06-11 10:49:41

How to add objects through admin panel using celery django?

They helped me on stack overflow to translate the code from models.py to tasks py so that it runs asynchronously, but now, due to overriding the save method, you can’t just add and update models through the

tasks.py admin

from celery import shared_task
    @shared_task
    def update_image(path, width, heigth):
        image = Image.open(path)
        output_size = (width, heigth)
        image.thumbnail(output_size)
        image.save()


models.py:

def save(self):
        task = update_image.delay(self.image.path, self.width, self.heigth)


views.py:

def put(self, request, pk):
            saved_content = get_object_or_404(Content.objects.all(), pk=pk)
            data = request.data.get('content')
            serializer = ContentSerializer(
                instance=saved_content, data=data, partial=True)
            if serializer.is_valid(raise_exception=True):
                content_saved = serializer.save()
            return Response({
                "success": "Picture '{}' updated successfully".format(content_saved.id)
            })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
noremorse_ru, 2020-06-11
@noremorse_ru

Who told you such a horror story?) if you now call the parent save() in save(), then we get recursion, you need to stick the task in the serializer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question