4

我收到上述警告

第 1 行的“用户名”列的数据被截断

我正在为我的模型“suserprofile”获取这个,其中“用户名”作为模型中我的第一个列出的字段

我检查了 max_length 选项,这对我来说不是问题,如果我更改模型中字段的顺序(将“email_id”字段更改为模型列表中的第一个),那么我会收到替换“用户名”的警告使用模型中我首先提到的任何字段(在本例中为 email_id)。

运行 syncdb 时我没有收到此警告,但仅当我尝试通过实际运行本地浏览器使用 django 将新对象保存在我的 views.py 文件中的模型(“suserprofile”)中时才收到警告。我不明白为什么会发生

编辑:

我的模型:

class SUserProfile(models.Model):
    email_id            = models.EmailField(max_length=30)
    username      = models.CharField(max_length=50,blank=True)  
    first_name          = models.CharField(max_length=30)
    last_name           = models.CharField(max_length=30)

我的观点

from django.allauth.models import SocialAccount
def profileview(request):
    user=request.user   
    if user.is_authenticated:
        a=SocialAccount.objects.filter(user=user)   
        try:    
            f=a.get(provider='facebook')
        except:
            f=None
    if f:
        fusername=f.get_provider_account    
        fdata=f.extra_data 
        ffirst_name=fdata['first_name']     
        flast_name=fdata['last_name']
        femail=fdata.get('email')
        try: 
            old_user=SUserProfile.objects.get(email_id=femail)
        except:
            new_user=SUserProfile( 
            username=fusername,
            email_id=femail,
            first_name=ffirst_name,
            last_name=flast_name,)
            new_user.save() 

运行浏览器时收到的警告详细信息:

 Exception Type:    Warning

 Exception Value:   Data truncated for column 'email_id' at row 1

 Exception Location:/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py in _warning_check, line 92

Traceback Switch to copy-and-paste view

usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response

       response = callback(request, *callback_args, **callback_kwargs)

...

▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py in _wrapped_view

       return view_func(request, *args, **kwargs)

...

▶ Local vars
/home/varun/webops/mysite/allauth/account/views.py in profileview

       new_user.save()

...

▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/base.py in save

self.save_base(using=using, force_insert=force_insert, force_update=force_update)

...

▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/base.py in save_base

result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)

...

▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py in _insert

       return insert_query(self.model, objs, fields, **kwargs)

...

▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py in insert_query

    return query.get_compiler(using=using).execute_sql(return_id)

...

 ▶ Local vars
 /usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py in execute_sql

    cursor.execute(sql, params)

...

 ▶ Local vars
 /usr/local/lib/python2.7/dist-packages/django/db/backends/util.py in execute

    return self.cursor.execute(sql, params)

...

▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py in execute

    return self.cursor.execute(query, args)

...

▶ Local vars
/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py in execute

    if not self._defer_warnings: self._warning_check()

...

▶ Local vars
/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py in _warning_check

     warn(w[-1], self.Warning, 3)

...

▶ Local vars 
4

2 回答 2

8

检查列格式和长度。在长度为 10 的 varchar 列中插入 20 个字符的字符串数据时,我遇到了同样的错误。

于 2013-06-20T05:29:04.093 回答
4

syncDB 不会覆盖现有的表/列定义。因此,如果您已经检查以确保文件中的长度正确,您可能需要检查您是否已重新制作表格 - 您需要删除并重新创建它们以便更新的列定义到位(我自己犯了几次这个错误)

于 2013-07-20T21:03:47.373 回答