在Django模板中显示属性筛选值

huangapple go评论80阅读模式
英文:

Showing property filter value in Django template

问题

I couldn't get the value of the LastActiveTrack in my template. Any ideas?

在我的模板中,我无法获取LastActiveTrack的值。有什么想法吗?

英文:

I have the follwoing models in my Django project:

  1. class File(models.Model):
  2. client = models.ForeignKey(User, on_delete=models.PROTECT, related_name='client_files')
  3. title = models.CharField(max_length=250, blank=True, null=True)
  4. ...
  1. class FileTracking(models.Model):
  2. file = models.ForeignKey(File, related_name='file_tracking', on_delete=models.CASCADE)
  3. description = models.CharField(max_length=250, null=True, blank=True)
  4. title = models.CharField(max_length=250, blank=True, null=True)
  5. date = models.DateField(auto_now=False, auto_now_add=False)
  6. active = models.BooleanField(default=False)
  7. ...

my view:

  1. class FileCurrentView(ListView):
  2. model = File
  3. template_name = 'files_current.html'
  4. def get_context_data(self, **kwargs):
  5. context = super().get_context_data(**kwargs)
  6. if self.request.user.is_authenticated:
  7. user=self.request.user
  8. current_files = files.filter(...)
  9. ...
  10. context["user"] = user
  11. context["current_files"]= current_files
  12. ...
  13. return context

In my template, I want to show the description or title of the last active record.

I tried to create a property in the FileTracking model:

  1. @property
  2. def LastActiveTrack(self):
  3. result = self.objects.filter(active=1).order_by("-date")[0].title
  4. result_txt = str(result)
  5. if result:
  6. return result_txt
  7. return ''

in my template:

  1. {% for file in current_files %}
  2. Title: {{file.title}}
  3. last Tracking: {{file.file_tracking.LastActiveTrack}}
  4. {% endif %}

but I couldn't get the value of the LastActiveTrack in my template. Any ideas?

答案1

得分: 0

在模板中这样做不太理想,因为你会为每个文件进行一次数据库调用,如果文件很多,这可能会导致不必要的开销。

更好的方法是更新你的初始调用,包括每个文件的文件跟踪信息,可以使用prefetch_related来实现,这应该限制为两次数据库调用。

  1. current_files = files.filter(...).prefetch_related(
  2. Prefetch(
  3. 'file_tracking',
  4. queryset=FileTracking.objects.order_by("-date"),
  5. # 为了在模板中明确引用,可以在Prefetch中添加一个to_attr参数
  6. to_attr="filetrack_ordered"
  7. )
  8. )

然后在你的模板中可以这样调用:

  1. {% for file in current_files %}
  2. Title: {{file.title}}
  3. Last Tracking: {{file.filetrack_ordered.0.title}}
  4. {% endfor %}

注意:在你的问题中,似乎你正在寻找file_tracking.title,但在你提供的模型字段中并没有实际提到。我假设它在模型中的某个地方。

  1. <details>
  2. <summary>英文:</summary>
  3. Doing this in your template is less than ideal as you will make a database call for each file, which may get expensive in overhead if you have a lot of files.
  4. Better to update your initial call to include each file&#39;s filtracking via [prefetch_related][1], which should limit it to two DB calls
  5. current_files = files.filter(...).prefetch_related(
  6. Prefetch(
  7. &#39;file_tracking&#39;,
  8. queryset=FileTracking.objects.order_by(&quot;-date&quot;),
  9. #to make it clear what we are referring to in our template
  10. #add a to_attr argument to Prefetch
  11. to_attr=&quot;filetrack_ordered&quot;
  12. )
  13. )
  14. then in your template you can call:
  15. {% for file in current_files %}
  16. Title: {{file.title}}
  17. last Tracking: {{file.filetrack_ordered.0.title}}
  18. {% endif %}
  19. NB: in your question you seem to be looking for file_tracking.title which isn&#39;t actually mentioned in the model fields you provide. I&#39;ve assumed it&#39;s in there somewhere.
  20. [1]: https://docs.djangoproject.com/en/4.2/ref/models/querysets/#prefetch-related
  21. </details>

huangapple
  • 本文由 发表于 2023年5月14日 09:34:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定