django-mptt / django-mptt

Utilities for implementing a modified pre-order traversal tree in django.
https://django-mptt.readthedocs.io/
Other
2.88k stars 467 forks source link

Running into strange cache issue #684

Open fyaconiello opened 5 years ago

fyaconiello commented 5 years ago

I am using 0.9.0 of django-mptt and 1.11.x of django core

Issue

After updating an MPTT managed model, I am getting cached old versions of content back from queries.

Note: I have memcache set up on my staging and production environments but have not set up any additional caching mechanism for this feature.

My model:

from django.db import models
from django.utils.translation import ugettext as _
from mptt.models import MPTTModel

class Category(MPTTModel):
    title = models.CharField(
        verbose_name = _(u'Title'),
        help_text = _(u' '),
        max_length = 255,
    )
    slug = models.SlugField(
        verbose_name = _(u'Slug'),
        help_text = _(u'Unique identifier.'),
        max_length = 255,
        unique = True,
    )
    parent = models.ForeignKey(
        'self',
        verbose_name = _(u'Parent'),
        null = True,
        blank = True,
        default = None
    )
    is_active = models.BooleanField(
        verbose_name = _(u'Is Active?'),
        help_text = _(u' Selectable in public forms'),
        default = False
    )
    order_by = models.IntegerField(
        verbose_name = _(u'Ordering Weight'),
        help_text = _(u'This item\'s weight within a list.'),
        default = 0
    )

    class MPTTMeta:
        order_insertion_by  = ['order_by', 'title']
        parent_attr = u'parent'

    class Meta:
        app_label = _(u'crm')
        verbose_name = _(u'Category')
        verbose_name_plural = _(u'Categories')

    def __unicode__(self):
        return u"%s" % (self.title, )

my admin looks like this:

class MyTreeRelatedFieldListFilter(TreeRelatedFieldListFilter):
    mptt_level_indent = 25

class CategoryAdmin(MPTTModelAdmin, VersionAdmin):
    prepopulated_fields = {'slug': ('title', )}
    list_display = ['title', 'is_active', ]
    list_filter = ['is_active', ('parent', MyTreeRelatedFieldListFilter)]
    search_fields = ['title', ]
    fieldsets = [
        (
            None,
            {
                'fields' : ('title', 'slug', 'parent', 'is_active', 'order_by')
            },
        ),
    ]

So if I had the following:

and then updated AA to be AA?? I still get AA back from Category.objects.filter(is_active=1)

matthiask commented 5 years ago

Hmm, what does the SQL query say? Which transaction isolation level (and database) are you using?

fyaconiello commented 5 years ago

I'm not doing anything crazy on my Database setup

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
...
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}

I'm using RDS's mysql equivelant.

Hmm, what does the SQL query say?

do you just want me to dump the generated sql? What are you looking for here?

My expectation is to use a normal django queryset for the mptt object and get the latest data from the tables, if there is some caching happening at the mptt level, I'd expect it to recache on object save.

The only way I'm able to get my queryset to return correctly what is in the db is to restart the uwsgi process with sudo service uwsgi restart which isn't something I should have to do to make querysets behave.

koztay commented 5 years ago

I am having the same issue, I see the deleted items which comes from some cache until I restart my docker containers... I am using django==2.1.7 and django-mptt==0.9.1

ajt commented 4 years ago

@koztay and @fyaconiello did you ever get this figured out? I am running into a similar issue.

yeosblue commented 4 years ago

Same here. I'm using django==2.2 & django-mptt==0.11.0