Search code examples
flaskpeeweeflask-admin

flask peewee how to change the field order when edit/creating forms


For a model User I want to put an inline model ProjectNotes in it, and how can I change the fields order when creating or editing it? For example, change the order to ProjectNotes, username, email.(See pic below.)

class User(BaseModel):
   username = peewee.CharField(max_length=80)
   email = peewee.CharField(max_length=120)

   def __unicode__(self):
       return self.username

class ProjectNotes(BaseModel):
   comment = peewee.CharField(max_length=64)
   user = peewee.ForeignKeyField(User)

   def __unicode__(self):
       return '%s - %s' % (self.comment)

class UserAdmin(ModelView):
   inline_models = (ProjectNotes,)

admin.add_view(UserAdmin(User))

enter image description here


Solution

  • You can pass additional attributes such as form_columns, form_label, column_labels for inline_models as a dictionary:

    class UserAdmin(ModelView):
        inline_models = [
            (ProjectNotes, {'form_columns': ('user', 'comment')})
        ]
    

    Or create form class for your ProjectNotes model:

    from flask_admin.model.form import InlineFormAdmin
    
    class ProjectNotesAdmin(InlineFormAdmin):
        form_columns = ('user', 'comment')
    
    class UserAdmin(ModelView):
        inline_models = [ProjectNotesAdmin(ProjectNotes)]
    

    I also found out that I need to specify primary key column in form_columns for flask_admin.contrib.sqla.ModelView. Don't know if you need to do the same thing for flask_admin.contrib.peewee.ModelView.