Does django list_details map a request to memory (not an update)?

I have a user model manager that looks like this:

class MyManager(models.Manager)
    def get_query_set(self):
        '''Only get items that are 'approved' and have a `pub_date` that is in
        the past.  Ignore the rest.'''
        queryset = super(MyManager, self).get_query_set()
        queryset = queryset.filter(status__in=('a',))
        return queryset.filter(pub_date__lte=datetime.utcnow())

And it works quite well; however, I have a problem using Django generic.list_detailviews object_detailand object_list: the query set seems to load only once, and because of this, it does not retrieve the elements that should be, because, I suppose, the utcnow()time is called only once (when it first loaded).

I assume this is intentional and implied as a performance improvement. However, this means that the video is displayed elsewhere on the site (in places where I am not in the view object_detail) before they are available in the view object_detail(see urls.py below). This results in 404s ...

Any ideas? Or do I need to write my own custom views to avoid this?

Thank!

urls.py

url(r'^video/(?P<object_id>\d+)$', 
    list_detail.object_detail,
    {   'queryset': Video.objects.all(), },
    name='video_detail',
),
+3
source share
3 answers

This is not a cache problem: as you do now, the definition of a set of requests is evaluated once, when parsing URLs, and then it is never evaluated again.

-: : , .
, .

, , .

+4

urls.py :

url(r'^video/(?P<object_id>\d+)$', 
    list_detail.object_detail,
    {   'queryset': Video.objects.all, }, # here the difference
    name='video_detail',
)

Edit:

, ( ) filter():

return queryset.filter(pub_date__lte=datetime.utcnow)
+2

thornomad .

, :

  • Video.objects.all
  • queryset.filter(pub_date__lte = datetime.utcnow), , , .

, , :

qs = lambda *x: Video.objects.all()
url(r'^video/(?P<object_id>\d+)$', 
    list_detail.object_detail,
    {   'queryset': qs(), },
    name='video_detail',
),

... , , , , , :)

lazy_qs = lambda *x: lazy(Post.live_objects.all, QuerySet)

blog_posts = {
    'queryset': lazy_qs(),

... ( ), utils.functional.lazy , QuerySet, .

, - , .

django docs , , ( , , !)

+1

Source: https://habr.com/ru/post/1724885/


All Articles