Errors related to memcached in django

MemcachedKeyCharacterError: Control characters not allowed. It seems that it may happen when you specify an object etc. as a key with the cache function of django.

models.py


from django.db import models

class TestModel(models.Model):
    test = models.CharField(verbose_name=u'test')

views.py


from django.core.cache import cache

def test_view(request):
    obj = TestModel.objects.get(pk=1)
    objs = cache.get(obj, None)

If you call the view above, you may get a'MemcachedKeyCharacterError: Control characters not allowed'error at'cache.get (obj, None)'.

models.py


from django.db import models

class TestModel(models.Model):
    test = models.CharField(verbose_name=u'test')

    def __unicode__(self):
        return u"%s:%s" % (self.__class__.__name__, self.id)

You can avoid the error by defining a character string with the method called when the object is output as described above.

Cache key contains characters that will cause errors if used with memcached

views.py


from django.core.cache import cache

def test_view(request):
    obj = TestModel.objects.get(pk=1)
    objs = cache.get("aaaa bbbb", None)

As mentioned above, keys including spaces are also NG.

Recommended Posts

Errors related to memcached in django
How to reflect CSS in Django
How to delete expired sessions in Django
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
Models in Django
Django related sites
Forms in Django
Dynamically add fields to Form objects in Django
How to implement Rails helper-like functionality in Django
I want to pin Datetime.now in Django tests
[Django] How to resolve errors when installing mysqlclient
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
How to deal with run-time errors in subprocess.call
Pass login user information to view in Django
I implemented Google's Speech to text in Django
How to create a Rest Api in Django
How to get multiple model objects randomly in Django
Set the form DateField to type = date in Django
How to use bootstrap in Django generic class view
TemplateView patterns you'll want to learn first in Django
Model changes in Django
unable to import django
How to resolve SSL module errors in Anaconda environment
How to upload files in Django generic class view
How to use Decorator in Django and how to make it
update django version 1.11.1 to 2.2
How to reference static files in a Django project
[Django] A pattern to add related records after creating a record
How to write custom validations in the Django REST Framework
How to use Laravel-like ORM / query builder Orator in Django
How to check ORM behavior in one file with django
How to update user information when logging in to Django RemoteUserMiddleware
Errors that often occur in django Part 1 About template.exceptions.TemplateDoesNotExist (Note)
I made a command to generate a table comment in Django
[Django] How to give input values in advance with ModelForm
How to generate a query using the IN operator in Django
Useful tricks related to list and for statements in Python
Display the time in Django according to the user's region (UTC)
[Django] I want to log in automatically after new registration
I can't log in to the admin page with Django3
For beginners, how to deal with common errors in keras
What to do when "Invalid HTTP_HOST header" appears in Django
In Django, how to abbreviate the long displayed string as ....
Django Changed to save lots of data in one go
Performance optimization in Django 3.xx
PHP var_dump in Django templates
Handle constants in Django templates
Covector to think in function
Implement follow functionality in Django
To flush stdout in Python
Rename table columns in Django3
Login to website in Python
Output table structure in Django
Shell to create django project
Speech to speech in python [text to speech]
(Note) Django in Vagrant environment
How to develop in Python
Pass text to Django genericview
Deploy django project to heroku