Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eezis/2781c623c1e848913dfd6f4a9a4b81a3 to your computer and use it in GitHub Desktop.
Save eezis/2781c623c1e848913dfd6f4a9a4b81a3 to your computer and use it in GitHub Desktop.
Django Migrations TypeError: attribute name must be string, not 'bytes'

If you are updating and older Django project and getting an error in this form:

TypeError: attribute name must be string, not 'bytes'

It's likely that you have existing migrations in your code base that have legacy byte strings. I found them in two places. In the related_name as shown below

('article', models.ForeignKey(related_name=b'author', to='core.Article', on_delete=models.CASCADE, null=True)),

And also in migrations where models had Choice fields. If you remove the byte strings from your migrations you should be good to go. Easiest way is to use a good code editor like VSCode and use the Edit/Find in Files feature to find all instances of b' and replace with a '... be prudent and visit each file for a visual inspection, do the find/replace one file at a time.

This article might be helpful if you want to clean up and reset you migrations after getting everything updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment