2

I have a model:

class Trophy(models.Model):
    server = models.CharField(max_length=191, blank=True)
    title = models.CharField(max_length=191, blank=True)
    note = models.TextField(blank=True)
    badge = models.CharField(max_length=191)

badge - is a field for unicode characters like or or or ☔️ or etc...

I have three trophies in database:

  1. Dragon (badge=)
  2. Antiaircrafter (badge=☔️)
  3. Diamond player (badge=)

I have not problem to get trophy with badge ☔️:

trophy = Trophy.objects.get(badge=u"☔️")

But I can't get or :

trophy = Trophy.objects.get(badge=u"")
MultipleObjectsReturned: get() returned more than one Trophy -- it returned 2!

And one more thing: I can't see and in MySQL Workbench (only "?") untill run:

SET NAMES utf8mb4;

Any idea how to work with utf8mb4 in django queries?

maksymov
  • 493
  • 9
  • 22
  • Have you tried this: https://stackoverflow.com/questions/39463134/how-to-store-emoji-character-in-my-sql-database – John Moutafis Jul 21 '17 at 10:15
  • What's confusing you about that error? You have two trophies with that badge. You can use `filter` instead of `get` to return them both. – Daniel Roseman Jul 21 '17 at 10:21
  • Daniel Roseman, I have one object with , one with and one with ☔️ – maksymov Jul 21 '17 at 10:59
  • 1
    One question mark or 4? See http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Jul 22 '17 at 04:49

1 Answers1

0

Fixed by adding OPTIONS for db settings in django settings.py:

'OPTIONS': {
     'charset': 'utf8mb4',
     'use_unicode': True,
     'init_command': "SET NAMES utf8mb4;",
 }

And create db with command

CREATE DATABASE feofun_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
maksymov
  • 493
  • 9
  • 22