我正在尝试使用AbstractUser向Django的标准用户模型添加字段.这是我的代码:
class GUser(AbstractUser):
uuid = UUIDField(auto=True)
Run Code Online (Sandbox Code Playgroud)
这是成功的,因为从壳我可以说,
>>> a = GUser.objects.all()
>>> a
[<GUser: User1>]
>>> a[0].uuid
UUID('7d1f0b7b52144a2ea20db81ce74741e3')
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是从/ admin注册一个新用户.当我创建一个新用户时,我收到一个数据库错误:
no such table: auth_user
Run Code Online (Sandbox Code Playgroud)
在我了解它之前,这是我的forms.py:
class GUserChangeForm(UserChangeForm):
class Meta:
model = get_user_model()
class GUserCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
def clean_username(self):
username = self.cleaned_data["username"]
try:
get_user_model().objects.get(username=username)
except get_user_model().DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate'])
Run Code Online (Sandbox Code Playgroud)
而我的admin.py:
class GUserAdmin(UserAdmin):
form = GUserChangeForm
add_form = GUserCreationForm
fieldsets = (
(None, {'fields': [('username', 'password')]}),
('Personal info', {'fields': ('is_active','is_staff','is_superuser','groups', 'user_permissions')}),
('Important dates', {'fields': ('last_login', 'date_joined')}),
#('Universal ID', …Run Code Online (Sandbox Code Playgroud) django django-forms django-admin django-authentication django-syncdb
我感到愚蠢,因为我一直试图访问这个响应变量一段时间,我想我不清楚闭包或范围,所以请帮助.
我正在研究chrome扩展,我正在将来自contentscript.js的消息发送到background.js并接收响应.现在我想返回响应并能够在contentscript.js中使用它.看起来像你应该做的事情......
function getWords(){
var words = [];
chrome.runtime.sendMessage({detail: "words"}, function(response) {
console.log(response) // prints ["word1, "word2" ..]
words = response;
});
return words; // = []
}
Run Code Online (Sandbox Code Playgroud)
更新:谢谢,我理解我现在的问题,但仍然想要一些建议来解决它.我的问题是,如果我需要将其作为另一个函数中的参数立即"请求"背景页面以获取单词列表,那么最好的方法是什么.我可以等待信息回来吗?我应该简单地从回调中调用其他函数吗?还是有其他方法吗?理想情况下,我想实际实现一个getWords(),直到列表返回后才返回...不可能?我也对开源库持开放态度.