简体   繁体   English

如何在views.py 中从我的数据库中获取内容? [姜戈]

[英]How to get the content from my database in views.py? [Django]

I am trying to print the content fields from my database, Here's my models.py file:我正在尝试从我的数据库中打印content字段,这是我的models.py文件:

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    read_time = models.TimeField(null=True, blank=True)
    view_count = models.IntegerField(default=0)

Here's my views.py file:-这是我的views.py文件:-

class PostDetailView(DetailView):
    model = Post
    def get_object(self):
        obj = super().get_object()
        obj.view_count += 1
        obj.save()
        return obj
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        all_texts = {
            'texts': context.content
        }
        print(all_texts[texts])
        return context

I am trying to access all the data's from the content field from my database, But the above way is not working, Is there any way I can access all the data's from the content field, because I have to perform some action on these fields, like calculate the read_time of any content, based on the length of it.我试图从我的数据库中访问content字段中的所有数据,但上述方法不起作用,有什么方法可以访问content字段中的所有数据,因为我必须对这些字段执行一些操作,就像根据内容的长度计算任何内容的read_time一样。

Just query all objects and loop the queryset to manipulate them according to your needs like so:只需查询所有对象并循环查询集以根据您的需要操作它们,如下所示:

def your_view(self, **kwargs):

    # Get all table entries of Model Post
    queryset = Post.objects.all()

    # Loop each object in the queryset
    for object in queryset:

    # Do some logic
        print(object.content)

    [...]
    return (..., ...)

You do not have to override the .get_queryset(…) method [Django-doc] for that, since the object is already passed to the context.不必重写.get_queryset(…)方法[Django的DOC]为,由于对象已经传递到环境。 You can simply render it in the template with:您可以简单地在模板中渲染它:

{{ object.content }}

In case you really need this in the context, you can implement this as:如果您在上下文中确实需要这个,您可以将其实现为:

class PostDetailView(DetailView):
    model = Post
    
    # …
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update(
            texts=self.object.content
        )
        return context

In case you need all post objects, you can add these to the context:如果您需要所有帖子对象,您可以将这些添加到上下文中:

class PostDetailView(DetailView):
    model = Post
    
    # …
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update(
            texts=self.object.content,
            posts=Post.objects.all()
        )
        return context

and render these as:并将这些渲染为:

{% for post in posts %}
    {{ post.content }}
{% endfor %}

It might be better to work with an F expression [Django-doc] when incrementing the view counter to avoid race conditions :在增加视图计数器以避免竞争条件时,最好使用F表达式 [Django-doc]

class PostDetailView(DetailView): model = Post def get_object(self): obj = super().get_object() views = obj.view_count obj.view_count = F('view_count') + 1 obj.save() obj.view_count = views+1 return obj class PostDetailView(DetailView): model = Post def get_object(self): obj = super().get_object() views = obj.view_count obj.view_count = F('view_count') + 1 obj.save() obj.view_count =视图+1返回对象

first import models首批进口车型

from . models import Post

then in your function然后在你的函数中

data=Post.objects.values('content').all()

Now data have all the values in content field data=[{'content':first_value},{'content':second_value},..like this...]现在数据具有内容字段中的所有值 data=[{'content':first_value},{'content':second_value},..like this...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM