1

I would like to get your help because I'm getting an issue which is a little bit weird to my mind.

I'm using Django 1.11.16

I have in my forms.py file this class :

class PublicationStatForm(forms.Form):
    # publication_list = forms.ModelMultipleChoiceField(queryset=Publication.objects.all().order_by('pub_id'))
    publication_list = forms.ModelMultipleChoiceField(
        queryset=Publication.objects.all().order_by('pub_id'),
        label=_('Publication Choice'),
        widget=ModelSelect2Widget(
            model=Publication,
            search_fields=['pub_id__icontains', 'title__icontains'],
            attrs={'data-placeholder': "Please select publication(s)"}
        )
    )

    def __init__(self, *args, **kwargs):
        super(PublicationStatForm, self).__init__(*args, **kwargs)

Then, in my views.py file :

class StatsView(TemplateView):
    """ Create statistics pageview """
    template_name = 'freepub/stats.html'
    form_class = PublicationStatForm

    def get_context_data(self, **kwargs):
        subtitle = _("Statistics")
        context_data = super(StatsView, self).get_context_data(**kwargs)
        context_data['form'] = self.form_class()
        ...
        return context_data

And finally in my template, I just have :

<form class="date-form" method="GET">
   <div class="row">
      <div class="col-md-7">
        {{ form.publication_list }}
      </div>
   </div>
   <input id="submit-date-stats" type="submit" class="btn btn-default" name="SearchPublicationPeriod"
               value="{% trans 'Submit' %}"/><br/>
</form>

I don't understand why, when I have this line in my form it works :

# publication_list = forms.ModelMultipleChoiceField(queryset=Publication.objects.all().order_by('pub_id'))

But when I replace this line by this :

publication_list = forms.ModelMultipleChoiceField(
    queryset=Publication.objects.all().order_by('pub_id'),
    label=_('Publication Choice'),
    widget=ModelSelect2Widget(
        model=Publication,
        search_fields=['pub_id__icontains', 'title__icontains'],
        attrs={'data-placeholder': "Please select publication(s)"}
    )
)

I get this issue :

Exception Type: ProgrammingError at /freepub/stats
Exception Value: relation "select_cache" does not exist
LINE 1: SELECT COUNT(*) FROM "select_cache"
                             ^

Do you have any idea ?

EDIT : Add cache settings

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-snowflake',
    },
    # "default": {
    #     "BACKEND": "django_redis.cache.RedisCache",
    #     "LOCATION": "redis://127.0.0.1:6379/1",
    #     "OPTIONS": {
    #         "CLIENT_CLASS": "django_redis.client.DefaultClient",
    #     }
    # },
    'select': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'select_cache',
        'TIMEOUT': None,
    }
}

# Set the cache backend to select2
SELECT2_CACHE_BACKEND = 'select'
Chris Wendel
  • 163
  • 1
  • 9
Essex
  • 6,042
  • 11
  • 67
  • 139
  • Have you run makemigrations and migrate? I see the code difference is in forms, but worth checking off first. – Withnail Dec 03 '18 at 14:26
  • 1
    I didn't touch to my models.py file but I can try. – Essex Dec 03 '18 at 14:26
  • I don't think it's likely to be that, but worth checking. What Cache backend do you have setup, could you include that? – Withnail Dec 03 '18 at 14:38
  • Yes I can, but I didn't handle cache setup during this process so I assume it doesn't come from that point ? – Essex Dec 03 '18 at 15:08
  • I think it might be, having been looking at the documentation earlier. (Nice library btw, haven't run across this one before.) . I think the issue is that your select2 cache backend should be `select2` not `select` - which makes sense with the error you're getting that `select_cache` doesn't exist. – Withnail Dec 03 '18 at 15:25
  • Missed a point off that - it's like the second cache you've defined there hasn't been created. edit: yes, realised what it is, answer coming. – Withnail Dec 03 '18 at 15:33

1 Answers1

1

The issue is with your cache settings.

Although you've set up a database cache backend, you need to run python manage.py createcachetable to actually create it in the database, similar to how you run migrate for regular model changes or additions - and is also why the error looks similar, which took me down the migrations route at first.

The Django ProgrammingError exception is an extension of the base DatabaseError, so it always relates to something wrong in the database. (source )

This wouldn't be the case if you use a redis or other KV store cache, as it will automatically insert it. Because the Django database cache uses a table in the same database, that table has to be created, per this section of the documentation.

Withnail
  • 3,128
  • 2
  • 30
  • 47
  • I don't have issue about cache thanks to your comment. I will validate it, but my form doesn't still work. Maybe I will create a new question. – Essex Dec 04 '18 at 10:55
  • Thanks! Hopefully i can help with that too. :) – Withnail Dec 04 '18 at 11:01