A
A
Antonov Dmitry2016-05-08 22:17:17
Django
Antonov Dmitry, 2016-05-08 22:17:17

Django tracking of viewed posts?

Hello!
It is necessary to implement the account of viewed posts for each user, I implement models.py like this:

class CountArticle(models.Model):
    user = models.OneToOneField(User)
    count = models.ManyToManyField(Article)

Added a new table with links between each user and articles.
This initialization takes place here context_processors.py:
def total_count(request):

    def init_count(request):
        #  add to CountArticle model Article list
        count = CountArticle.objects
        usr = request.user
        art = Article.objects.all()
        art_add = Article.objects
        count.create(user=usr)
        count = CountArticle.objects.get(user=usr)
        for x in art:
            count.count.add(art_add.get(article_title=x))
        return {"total_count": len(count.count.all()), }

    if request.user.is_authenticated():
        try:
            total = CountArticle.objects.get(user=request.user)
            return {"total_count": len(total.count.all()), }
        except ObjectDoesNotExist:
            return init_count(request)
    return {}

The essence of the problem: I register a user, the initialization of my viewed articles occurs on the first count request, i.e. almost immediately after registration, and now I have added 20 articles and it seems to me that it all starts to be processed for a very long time, but what if I have 1000 articles?
Please tell me the options for implementing this function, or maybe the approach is correct, but I'm doing something wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-05-09
@BlackTrub

sqlite - a database for development, no one puts it in production Measure
the speed on it, seriously, no one does it anymore https://docs.djangoproject.com/en/1.9/topics/auth/...
techydiary.com /how-to-customize-user-model-in-django1-5 field name does not match the content "initialize" m2m is not necessary

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question