Monday, January 16, 2017

Re: Odd problem: some database updates do not appear on other pages until server restart

Bob,

On 8/19/16 8:02 AM, bobhaugen wrote:
> On Friday, August 19, 2016 at 5:20:45 AM UTC-5, Michal Petrucha wrote:
>
> Moving the call to with_user to form.__init__ solved the problem in the form ModelChoiceField.
>
> These questions remain unanswered, although I intend to do a bunch more testing:
>
>     How pervasive is this problem? Does it affect template variables like {{ object.foreign_key_method }} where the foreign_key_method returns a queryset?

I think this is very pervasive.  That is, it happened a few times
to my team until we educated all developers to never use the
choices, limit_choices_to, or queryset parameters of a Model
field or Form field.  Instead, we always explicitly set them in
__init__() instead.

I assume other teams also make this mistake often.

For us, this mistake caused 3 separate type of problems:

1. The problem you described:
    Old data from the DB was "cached" until we restarted the
    Django server, because the DB query was done only
    when the Model or Form class was defined, not when each
    Model or Form instance was created.  So, in answer to
    your question above, yes, it would also affect template
    variables that refer to the data that was loaded from the
    DB when the class was defined.

2. Occasional import loops, which prevented the Django
    server from starting the app, because the value of the
    choices, limit_choices_to, or queryset parameter caused
    another model to be loaded.  This would trip us up badly
    because the import loop would often occur in our TEST or
    PROD environments (Django loaded via Apache WSGI) but
    not in our DEV environments (Django DEV server), since
    the order of class imports could be different in those 2
    different configurations.  See:
    - https://groups.google.com/forum/#!topic/django-users/ONGCSO37UWY

3. There's a warning here about this mistake being able to
    pollute your TEST execution with PROD data:
    - https://docs.djangoproject.com/en/dev/topics/testing/overview/#the-test-database

>     Is this behavior clearly documented anywhere?

It didn't jump out at us.  We got burned by and it and put in
some time to diagnose it.  Then we found the above link that
warns of one of the problems.

--Fred

Fred Stluka -- mailto:fred@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.


No comments:

Post a Comment