python - 一旦我对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
本质上,一旦用户发布了帖子,它就会被发送到一个异步任务,我在其中更改模型中的一个属性。模型已正确更新,但我希望在数据库更新后更改详细信息视图。这可能吗?
解决方案
推荐阅读
- r - 我正在尝试从每日数据中获取季节性融化信息。不能按季节汇总
- javascript - 使用 jQuery 验证 Vtiger CRM 6.2 字段(自定义模块)
- python - 将 bash 变量传递给 python 命令
- apache-kafka - Kafka KStream-KTable 加入竞争条件
- python-3.x - 如何在 RESTful Flask/SQLAlchemy/Marshmallow 环境中读取 GET 请求的参数
- c - SDCC / GBDK,将结构数组传递给函数
- sql - 如何在sql中找到两个时间值之间的差异
- python - 当 t[0] != 0 时从 signal.lsim 得到奇怪的结果
- php - 如何创建组合的 LETTER + ACCENT 像(e + ̀),在 PHP 中看起来像 è
- image - 您可以将 docker 映像转换/构建为完整的 OS 映像吗?