我想扩展django中的自定义用户模型。我从django官方网站复制粘贴代码。当我想迁移它时会抛出错误
TypeError: expected string or buffer
models.py
education=models.CharField(max_length=13)
from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
class MyUserManager(BaseUserManager):
def create_user(self, email, date_of_birth, password=None):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
date_of_birth=date_of_birth,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, date_of_birth, password):
user = self.create_user(email,
password=password,
date_of_birth=date_of_birth
)
user.is_admin = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
date_of_birth = models.CharField(max_length=20)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth']
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
def __unicode__(self): # __unicode__ on Python 2
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
return True
@property
def is_staff(self):
"Is the user a member of staff?"
return self.is_admin
管理员
from django import forms
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from .models import MyUser
class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = MyUser
fields = ('email', 'date_of_birth')
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField()
class Meta:
model = MyUser
fields = ('email', 'password', 'date_of_birth', 'is_active', 'is_admin')
def clean_password(self):
return self.initial["password"]
class UserAdmin(BaseUserAdmin):
form = UserChangeForm
add_form = UserCreationForm
.
list_display = ('email', 'date_of_birth', 'is_admin')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal info', {'fields': ('date_of_birth',)}),
('Permissions', {'fields': ('is_admin',)}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'date_of_birth', 'password1', 'password2')}
),
)
search_fields = ('email',)
ordering = ('email',)
filter_horizontal = ()
admin.site.register(MyUser, UserAdmin)
admin.site.unregister(Group)
错误回溯
Rendering model states... DONE
Applying app.0002_auto_20160420_0647...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 176, in add_field
self._remake_table(model, create_fields=[field])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 74, in _remake_table
self.effective_default(field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 207, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1322, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1317, in get_prep_value
return self.to_python(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1274, in to_python
parsed = parse_date(value)
File "/usr/local/lib/python2.7/dist-packages/django/utils/dateparse.py", line 60, in parse_date
match = date_re.match(value)
TypeError: expected string or buffer
0002_auto_20160420_0647.py的内容
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('app', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='myuser',
options={},
),
migrations.AlterModelManagers(
name='myuser',
managers=[
],
),
migrations.RemoveField(
model_name='myuser',
name='contact',
),
migrations.RemoveField(
model_name='myuser',
name='date_joined',
),
migrations.RemoveField(
model_name='myuser',
name='education',
),
migrations.RemoveField(
model_name='myuser',
name='first_name',
),
migrations.RemoveField(
model_name='myuser',
name='groups',
),
migrations.RemoveField(
model_name='myuser',
name='is_staff',
),
migrations.RemoveField(
model_name='myuser',
name='is_superuser',
),
migrations.RemoveField(
model_name='myuser',
name='last_name',
),
migrations.RemoveField(
model_name='myuser',
name='name',
),
migrations.RemoveField(
model_name='myuser',
name='user_permissions',
),
migrations.RemoveField(
model_name='myuser',
name='username',
),
migrations.AddField(
model_name='myuser',
name='date_of_birth',
field=models.DateField(default=1),
preserve_default=False,
),
migrations.AddField(
model_name='myuser',
name='is_admin',
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name='myuser',
name='email',
field=models.EmailField(unique=True, max_length=255, verbose_name=b'email address'),
),
migrations.AlterField(
model_name='myuser',
name='is_active',
field=models.BooleanField(default=True),
),
]
您的问题是迁移文件中的以下行:
field=models.DateField(default=1),
更改为
field=models.DateField(null=True),
要么
field=models.CharField(max_length=20),
问题内容: 需要帮忙。有一个名为arglist的数据列表,例如:[‘dlink’,’des’,‘1210’,’c’,24] <-这就是“打印”视图。 这段代码: 它给: 怎么了? 问题答案: 当json.loads需要一个字符串时,您正在尝试加载文件对象。您可以使用 或者更好: 在第一个示例中,文件是打开的,但从未关闭(不好的做法)。在第二个示例中,上下文管理器在离开上下文块后关闭文件。
问题内容: 我在将值写入文件时遇到以下错误。您能帮我弄清楚问题出在哪里以及如何解决吗? 错误: 问题答案: 假设您只想将字符串写入文件,请更改为或issue 。
我以不同的方式尝试了以下代码,比如去掉while或if,但是当我把它们放在一起(if和while)时,我总是在最后得到错误。。。 错误: 通过将变量“a”的声明方式更改为: *注意,这里的显著变化是使用VARCHAR2而不是CHAR(不是更大的长度)。根据@user272735的回答,这是关键。
我在游标循环中连接varchar2数据类型时遇到了问题。 过程在循环中迭代,以生成用于批处理的插入和删除操作的in子句。该过程将对每1000个帐号批处理运行。 有没有其他方法可以做到这一点?
本文向大家介绍Java中的字符串缓冲区和字符串生成器之间的区别,包括了Java中的字符串缓冲区和字符串生成器之间的区别的使用技巧和注意事项,需要的朋友参考一下 字符串缓冲区和StringBuilder都是可变类,可用于对字符串对象执行操作,例如字符串的反向,压缩字符串等。我们可以在不创建字符串新对象的情况下修改字符串。字符串缓冲区是线程安全的,而字符串生成器不是线程安全的。因此,它比字符串缓冲区快
问题内容: 我有一个名为Memcached.Js的项目,它是Memcached服务器到Node.js的端口。 我一直在使用字符串和缓冲区进行比较,比较内存占用量和性能。对于内存,毫无疑问,缓冲区是正确的选择。 但令我惊讶的是,表演并非如此。执行字符串操作比使用缓冲区更快。这是我尝试的: 完整的代码在这里:https : //github.com/dalssoft/memcached.js/blob