首页 > 解决方案 > 一旦我对views.py之外的模型进行了更改,有没有办法可以动态更新Django的详细视图?

问题描述

这是我目前所拥有的。

视图.py

class MultipleModelView(generic.TemplateView):
    template_name = 'upload/home.html'

    # @login_required(redirect_field_name='notloggedin.html')

    def get_context_data(self, **kwargs):
        if self.request.user.is_authenticated:
             context = super(MultipleModelView, self).get_context_data(**kwargs)
             context['jobs'] = Job.objects.filter(author=self.request.user).order_by('-created_at')
             context['posts'] = devpost.objects.all().order_by('-created_at')
             context['subcounter'] = SubCounter()
             context['inprogcounter'] = InProgCounter()
             context['fincounter'] = FinCounter()
             return context
        else:
            context = super(MultipleModelView, self).get_context_data(**kwargs)
            # context['jobs'] = Job.objects.filter(author=self.request.user).order_by('-created_at')
            context['posts'] = devpost.objects.all().order_by('-created_at')
            return context

class PostCreateView(LoginRequiredMixin,generic.CreateView):
    model = Job
    fields = ['patient_name', 'location']

    def form_valid(self, form):
        form.instance.author = self.request.user
        id = randomStringDigits(5)
        form.instance.identifier = id
        sleepy.delay(id=id)
        return super().form_valid(form)

之所以看起来像这样,是因为我有一个页面,上面有多个模型。

作业模型在创建后在此文件中更新。

任务.py

@shared_task
def sleepy(id=None):
    MyModel = apps.get_model('upload','Job')
    working_with = MyModel.objects.get(identifier=id)
    working_with.status = "In Progress"
    sleep(30)
    working_with.status = "Finished"
    return None

本质上,一旦用户发布了帖子,它就会被发送到一个异步任务,我在其中更改模型中的一个属性。模型已正确更新,但我希望在数据库更新后更改详细信息视图。这可能吗?

标签: pythondjangodjango-modelsdjango-rest-frameworkdjango-views

解决方案


推荐阅读