使主键字段在Flask-Admin中可编辑 [英] Make primary-key fields editable in Flask-Admin
问题描述
用户
为用户
。然而,Flask-Admin并没有在模型的创建/编辑页面中显示这些字段。 现在,当我尝试创建一个新用户时,Save peewee.UserDoesNotExist
错误,Save& Add表示记录已成功创建两次,但实际上并没有做任何事情。
我已经扩展了 save()
方法来自动生成名称的用户名,如果它没有设置的话,即使我删除了覆盖。
$ b
代码...
以下是我的User模型:
#import peewee as pw
$ b $ class User(BaseModel,UserMixin):
username = pw.CharField(32,primary_key = True)
password = pw.CharField(512,null = True)
name = pw.CharField(64)
# ..其他字段不显示...
def save(self,* args,** kwargs):
#设置用户名字段为空
if s elf.username =='auto'or not self.username:
self.username = self.name.replace('','').lower()
#真正保存
super(User,self).save(* args,** kwargs)
这是我的管理员代码:
#from flask_admin.contrib.peewee.view import ModelView
$ b $ class AdminModelUser(ModelView):
can_create = True
column_list =('username','name','group','active')
admin.add_view(AdminModelUser(User,name ='Users ',category ='Accounts'))
试用东西 p>
我后来尝试覆盖 现在显示字段,但保存仍然不起作用。当我编辑现有用户的用户名,管理员说记录已成功保存,但它不会被保存。而当我尝试创建一个新的用户,我仍然得到一个 我的猜测是我已经在错误的地方做了压倒一切,领域显示的形式,但没有在保存方法。在文档中我找不到任何提及:有人知道怎么做吗? get_form()
方法,使用 wtfpeewee $ c
$ p $ #from wtfpeewee.orm import model_form $ b $ class AdminModelUser($ c $> $ c>)并允许pk,如下所示: ModelView):
...
def get_form(self):
return model_form(User,a llow_pk = True)
peewee.UserDoesNotExist
错误。
force_insert = True
来调用 save()
来添加一个新行。
I am using Flask-Admin for my Flask-based project. In it, I have some models (using peewee) where the primary-key is user-set, such as username
for a User
. However Flask-Admin is not showing these fields in the model's create/edit pages.
Now, when I try to create a new user, the "Save" button gives a peewee.UserDoesNotExist
error, and the "Save & Add" says "Record successfully created" twice but doesn't actually do anything.
I had extended the save()
method to auto-generate the username from the name if it's unset, but the problem persisted even when I removed the overriding.
The code...
Here's what my User model looks like:
# import peewee as pw
class User(BaseModel, UserMixin):
username = pw.CharField(32, primary_key=True)
password = pw.CharField(512, null=True)
name = pw.CharField(64)
# ... other fields not shown ... #
def save(self, *args, **kwargs):
# Set the username if field is blank
if self.username == 'auto' or not self.username:
self.username = self.name.replace(' ', '').lower()
# Do the real save
super(User, self).save(*args, **kwargs)
Here's my admin code:
# from flask_admin.contrib.peewee.view import ModelView
class AdminModelUser(ModelView):
can_create = True
column_list = ('username', 'name', 'group', 'active')
admin.add_view(AdminModelUser(User, name='Users', category='Accounts'))
Trying out stuff
I later tried to override the get_form()
method, to use wtfpeewee
directly and allow the pk, like this:
# from wtfpeewee.orm import model_form
class AdminModelUser(ModelView):
...
def get_form(self):
return model_form(User, allow_pk=True)
Now the field is showing, but saving still does not work. When I edit the username of an existing user, the admin says "Record was successfully saved", but it doesn't get saved. And when I try to create a new user, I still get a peewee.UserDoesNotExist
error.
My guess is that I've done the overriding in the wrong place, with the fields showing in the form but not in the save methods. I couldn't find any mention of this in the docs: does anyone know how to do it?
When you've got a non-integer primary key, you must call save()
with force_insert=True
to add a new row.
这篇关于使主键字段在Flask-Admin中可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!