运行命令manage.py migrate

时间:2017-06-13 20:40:59

标签: python django django-models

我更改了我的models.py,之后我无法进行python manage.py migrate,我运行此命令时出错。我不明白这个字符串值是什么Incorrect string value: '\\xD0\\x90\\xD0\\xB4\\xD0\\xB4...'在Traceback中写了这个字符串的列名,但我的数据库中没有列名! 这是我的models.py:

from django.db import models
from datetime import datetime, timedelta, date
from django.utils import timezone
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
from .sitemap import add_url

# Create your models here.

class Server(models.Model):
    title = models.CharField(verbose_name='Название', max_length=255, unique=True)
    client = models.CharField(verbose_name='Клиент',max_length=255)
    rate = models.CharField(verbose_name='Рейт',max_length=255)
    online = models.IntegerField(verbose_name='Количество онлайн',blank=True, null=True, default=0)
    description = models.TextField(verbose_name='Описание',blank=True, null=True)
    price_for_gold = models.IntegerField(verbose_name='Цена за золото',)
    lvl_up_1_10 = models.IntegerField(verbose_name='Прокачка 1-10',default=0)
    lvl_up_10_20 = models.IntegerField(verbose_name='Прокачка 10-20',default=0)
    lvl_up_20_30 = models.IntegerField(verbose_name='Прокачка 20-30',default=0)
    lvl_up_30_40 = models.IntegerField(verbose_name='Пркоачка 30-40',default=0)
    lvl_up_40_50 = models.IntegerField(verbose_name='Прокачка 40-50',default=0)
    lvl_up_50_60 = models.IntegerField(verbose_name='Прокачка 50-60',default=0)
    lvl_up_60_70 = models.IntegerField(verbose_name='Прокачка 60-70',blank=True, null=True)
    link = models.CharField(max_length=255, verbose_name='Ссылка', default='', unique=True)

    def __str__(self):
        return self.title

    def get_image_url(self):
        image = Image_for_server.objects.get(server=self)
        return image.image.url

    class Meta:
        verbose_name = 'Сервер'
        verbose_name_plural = 'Сервера'

class Discount(models.Model):
    TYPE_CHOICES = (
        ('Золото', 'Золото boost'),
        ('Прокачка уровня', 'Прокачка уровня %')
    )
    start = models.IntegerField(verbose_name='Со скольки')
    count = models.IntegerField(verbose_name='Какая скидка')
    type = models.CharField(verbose_name='Тип скидки', max_length=255, choices=TYPE_CHOICES)
    server = models.ForeignKey('Server', verbose_name='Сервер', on_delete=models.CASCADE)

    def __str__(self):
        return 'Server - {0} discount from {1}'.format(self.server.title, self.start)

    class Meta:
        verbose_name = 'Скидка'
        verbose_name_plural = 'Скидки'

class Order(models.Model):
    PAYMENT_CHOICES = (
        ('Оплачено', 'Оплачено'),
        ('Не оплачено','Не оплачено')
    )
    EXECUTION_CHOICES = (
        ('Выполнено','Выполнено'),
        ('Не выполнено','Не выполнено')
    )
    status_of_execution = models.CharField(verbose_name='Статус заказа', max_length=255, choices=EXECUTION_CHOICES, default='Не оплачено')
    status_of_payment = models.CharField(verbose_name='Статус оплаты', max_length=255, choices=PAYMENT_CHOICES, default='Не выполнено')
    client_comment = models.TextField(verbose_name='Комментарий клиента', blank=True, null=True)
    admin_comment = models.TextField(verbose_name='Комментарий администратора', blank=True, null=True)

    class Meta:
        verbose_name = 'Заказ'
        verbose_name_plural = 'Заказы'

class Ordered_lvl_up(models.Model):
    lvl_start = models.IntegerField(verbose_name='С какого уровня', default=0)
    lvl_end = models.IntegerField(verbose_name='По какой уровень', default=0)
    order = models.ForeignKey('Order',verbose_name='Заказ',  on_delete=models.CASCADE)
    server = models.ForeignKey('Server',verbose_name='Сервер',  on_delete=models.CASCADE)
    price = models.IntegerField(verbose_name='Цена', )

    class Meta:
        verbose_name = 'Прокачка уровня в заказе'
        verbose_name_plural = 'Прокачка уровня в заказе'

class Ordered_gold(models.Model):
    count = models.IntegerField(verbose_name='Количество', default=0)
    order = models.ForeignKey('Order',verbose_name='Заказ',  on_delete=models.CASCADE)
    server = models.ForeignKey('Server',verbose_name='Сервер',  on_delete=models.CASCADE)
    price = models.IntegerField(verbose_name='Цена', )

    class Meta:
            verbose_name = 'Золото в заказе'
            verbose_name_plural = 'Золото в заказе'

class Ordered_characters(models.Model):
    order = models.ForeignKey('Order',verbose_name='Заказ',  on_delete=models.CASCADE)
    character = models.ForeignKey('Character',verbose_name='Персонаж',  on_delete=models.CASCADE)
    price = models.IntegerField(verbose_name='Цена', )

    class Meta:
        verbose_name = 'Персонаж в заказе'
        verbose_name_plural = 'Персонажи в заказе'

class Ordered_addons(models.Model):
    character = models.ForeignKey('Ordered_characters',verbose_name='Персонаж в заказе',  on_delete=models.CASCADE)
    price = models.IntegerField(verbose_name='Цена', )
    title = models.CharField(verbose_name='Название', max_length=255)

    class Meta:
        verbose_name = 'Аддон в заказе'
        verbose_name_plural = 'Аддоны в заказе'

class Ordered_products(models.Model):
    order = models.ForeignKey('Order',verbose_name='Заказ',  on_delete=models.CASCADE)
    product = models.ForeignKey('Product',verbose_name='Продукт',  on_delete=models.CASCADE)
    price = models.IntegerField(verbose_name='Цена', )

    class Meta:
        verbose_name = 'Продукт в заказе'
        verbose_name_plural = 'Продукты в заказе'

class Product(models.Model):
    server = models.ForeignKey('Server',verbose_name='Сервер',  on_delete=models.CASCADE)
    title = models.CharField(verbose_name='Название', max_length=255, unique=True)
    description = models.CharField(max_length=255, verbose_name='Краткое описание', blank=True, null=True, default='')
    price = models.IntegerField(verbose_name='Цена', )
    price_with_discount = models.IntegerField(verbose_name='Цена со скидкой', blank=True, null=True)
    link = models.CharField(max_length=255, verbose_name='Ссылка', default='', unique=True)

    def get_image_url(self):
        image = Image_for_product.objects.get(product=self)
        return image.image.url

    class Meta:
        verbose_name = 'Товар'
        verbose_name_plural = 'Товары'

class Image_for_product(models.Model):
    product = models.ForeignKey('Product',verbose_name='Продукт',  on_delete=models.CASCADE)
    image = models.ImageField(verbose_name='Изображение', upload_to='core/', default='')

    class Meta:
        verbose_name = 'Изображение'
        verbose_name_plural = 'Изображения'

class Character(models.Model):
    FRACTION_CHOICES = (
        ('Alliance','Alliance'),
        ('Horde','Horde')
    )
    title = models.CharField(verbose_name='Название', max_length=255, unique=True)
    description = models.TextField(verbose_name='Описание', blank=True, null=True)
    server = models.ForeignKey('Server',verbose_name='Сервер',  on_delete=models.CASCADE)
    fraction = models.CharField(verbose_name='Фракция', max_length=255, choices=FRACTION_CHOICES)
    characters_class = models.CharField(verbose_name='Класс', max_length=255)
    lvl = models.IntegerField(verbose_name='Уровень', )
    price = models.IntegerField(verbose_name='Цена', )
    price_with_discount = models.IntegerField(verbose_name='Цена со скидкой', default=0)
    date_of_end_discount = models.DateTimeField(verbose_name='Окончание скидки', )
    avg_voice = models.DecimalField(verbose_name='Средний голос', max_digits=11, decimal_places=2)
    count_of_voice = models.IntegerField(verbose_name='Количество голосов', )
    link = models.CharField(max_length=255, verbose_name='Ссылка', default='', unique=True)

    def get_image_url(self):
        image = Image_for_characters.objects.get(character=self)
        return image.image.url

    def is_discount(self):
        return self.date_of_end_discount > timezone.now()

    class Meta:
        verbose_name = 'Персонаж'
        verbose_name_plural = 'Персонажи'

    def __str__(self):
        return self.title

class Technical_parameter(models.Model):
    key = models.CharField(verbose_name='Ключ', max_length=255)
    value = models.CharField(verbose_name='Значение', max_length=255)
    characters = models.ForeignKey('Character',verbose_name='Персонаж',  on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Техническое описание'
        verbose_name_plural = 'Техническое описание'

class Related_character(models.Model):
    character = models.ForeignKey('Character',verbose_name='Персонаж',  on_delete=models.CASCADE, related_name='main_characters')
    related_character = models.ForeignKey('Character',verbose_name='Похожий персонаж',  on_delete=models.CASCADE, related_name='related_characters')

    class Meta:
        verbose_name = 'Похожий персонаж'
        verbose_name_plural = 'Похожие персонажи'

class Addon(models.Model):
    price = models.IntegerField(verbose_name='Цена', )
    title = models.CharField(verbose_name='Название', max_length=255)
    character = models.ForeignKey('Character',verbose_name='Персонаж',  on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Аддон'
        verbose_name_plural = 'Аддоны'

class Voice(models.Model):
    character = models.ForeignKey('Character',verbose_name='Персонаж',  on_delete=models.CASCADE)
    mark = models.IntegerField(verbose_name='Оценка')
    ip = models.CharField(verbose_name='Ip', max_length=255)

    class Meta:
        verbose_name = 'Голос'
        verbose_name_plural = 'Голоса'

class Image_for_characters(models.Model):
    character = models.ForeignKey('Character',verbose_name='Персонаж',  on_delete=models.CASCADE)
    image = models.ImageField(verbose_name='Изображение', upload_to='core/', default='')

    class Meta:
        verbose_name = 'Изображение'
        verbose_name_plural = 'Изображения'

class Image_for_server(models.Model):
    server = models.ForeignKey('Server', verbose_name='Сервер', on_delete=models.CASCADE)
    image = models.ImageField(verbose_name='Изображение', upload_to='core/', default='')

    class Meta:
        verbose_name = 'Изображение'
        verbose_name_plural = 'Изображения'


# @receiver(post_save, sender=Server)
# def server_save_handler(sender, **kwargs):
#     if kwargs['created']:
#         add_url('/' + kwargs['instance'].link, "1")
#
# @receiver(post_save, sender=Character)
# def character_save_handler(sender, **kwargs):
#     if kwargs['created']:
#         add_url('/' + kwargs['instance'].server.link + '/' + kwargs['instance'].link, "0.9")

这是我的追踪文字:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 227, in handle
    self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/core/management/sql.py", line 53, in emit_post_migrate_signal
    **kwargs
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 193, in send
    for receiver in self._live_receivers(sender)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 193, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/contrib/auth/management/__init__.py", line 83, in create_permissions
    Permission.objects.using(using).bulk_create(perms)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/db/models/query.py", line 442, in bulk_create
    ids = self._batched_insert(objs_without_pk, fields, batch_size)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/db/models/query.py", line 1083, in _batched_insert
    self._insert(item, fields=fields, using=self.db)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/db/models/query.py", line 1060, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
    cursor.execute(sql, params)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
  File "/home/vladyslav/Python/wow/wowenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 292, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1366, "Incorrect string value: '\\xD0\\x90\\xD0\\xB4\\xD0\\xB4...' for column 'name' at row 1")

修改 我从数据库中删除所有表并删除所有迁移,之后一切都很好!

3 个答案:

答案 0 :(得分:0)

我认为问题在于您的数据库配置。似乎db不在utf-8中。或者你的代码中有一个无效的utf-8字符串。

答案 1 :(得分:0)

错误来自Python无法解码模型定义中使用的字符。我100%确定这是与您的verbose_name财产相关的错误。尝试将它们评论出来并确定是否存在问题。

答案 2 :(得分:0)

您应该将此行添加到models.py

的开头
# -*- coding: utf-8 -*-