简体   繁体   English

Django将模型外键默认值应用于现有记录

[英]Django Apply Model Foreign Key Default Value to Existing Record

Thank you very much for taking your time. 非常感谢您抽出宝贵的时间。

Previously, I posted this question, How to Get Unread Posts for Users . 以前,我发布了这个问题, 如何为用户获取未读帖子

The problem was: I cannot filter out which article one user has not read while this article has already been read by another user. 问题是:当另一位用户已经阅读了这篇文章时,我无法过滤掉一位用户没有阅读过的文章。

I figured out why I cannot do that---- because I have data that I wrote into the database without using Django although I set the reading default for each post to False---- there is simply no record. 我弄清楚了为什么我不能做到这一点-因为尽管我将每篇文章的阅读默认设置都设置为False-尽管我没有将Django写入数据库的数据,但是却没有记录。

Then, I manually set one article to unread to a user in the Admin, everything works, because now in the database there is one record stating that this certain article has not been read by this user. 然后,我在Admin中手动将一篇文章设置为未读给用户,因为一切都正常了,因为现在数据库中有一条记录表明该用户尚未阅读该文章。

The problem now is: 现在的问题是:

How do I set "unread" for every existing article that I have in the database for all existing users? 如何为数据库中所有现有用户为我现有的每篇文章设置“未读”?

And how can articles stay unread for every new user unless the new user actually read it? 除非新用户实际阅读文章,否则如何使每个新用户都无法阅读文章?

For your convenience, I copied the codes to here. 为了方便起见,我将代码复制到此处。

My model: 我的模特:

class Posts(models.Model):
 title = models.CharField(max_length=255)
 content_url = models.URLField(unique=True)
 content = models.CharField(max_length=255)
 post_date = models.DateField(default="2999-12-12")

Another 另一个

class readstatus(models.Model):
 reading_status = models.BooleanField(default=False)
 user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
 article = models.ForeignKey(Posts, on_delete=models.CASCADE)
 created = models.DateTimeField(auto_now_add=True)

My View: 我的观点:

class DailyReading(ListView):

 template_name = 'reading.html'
 context_object_name = "data" 
 paginate_by = 20
 def get_queryset(self):

    if self.kwargs['status'] == "read":
        queryset = piaoyou.objects.filter(readstatus__reading_status=True,readstatus__user=self.request.user)
        return queryset
    else:
        queryset= Posts.objects.filter(readstatus__reading_status=False,readstatus__user=self.request.user)
        return queryset

My Template: 我的模板:

    {% for info in data %}
                <quoteblock>
                    <ul>
                        <li><a href="{{ info.get_absolute_url }}">{{ info.title }}
                            <footnote></footnote>
                        </a>{{ info.post_date }}</li>

                        <footnote>{{ info.get_abstract_content }}</footnote>
                    </ul>
                </quoteblock>

       {% endfor %}

OMG, I just figured out how to do this. 天哪,我只是想出了怎么做。

For an article that has been read by the requesting user, we can just pass queryset = Post.objects.filter(readstatus__reading_status=True, readstatus__user=self.request.user) 对于请求用户已阅读的文章,我们只需传递queryset = Post.objects.filter(readstatus__reading_status = True,readstatus__user = self.request.user)

For an article that has not been read by the requesting user yet, we can pass Posts.objects.all() into the template. 对于请求用户尚未阅读的文章,我们可以将Posts.objects.all()传递到模板中。

Then in the template: 然后在模板中:

We need {% if instance.readstatus_set.all %} this line to make things work. 我们需要{%if instance.readstatus_set.all%}这一行来使工作正常。 Assume there is no data about whether this requesting user has read the article, the instance should not have readstatus_set.all, which means the user has not read the article yet. 假设没有有关此发出请求的用户是否已阅读文章的数据,该实例不应具有readstatus_set.all,这意味着该用户尚未阅读该文章。 Once checking this if condition, we can carry out to check other conditions in the loop. 一旦检查了这个if条件,我们就可以执行检查循环中的其他条件。

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

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