syrusakbary/Flask-SuperAdmin

Model view has fields out of order.

brotatos opened this issue · 2 comments

Originally, I thought the view would alphanumerically sort the fields but it appears to be quite random.

Here's the schema for a post object:

class Post(db.Model):                                            
    id = db.Column(db.Integer, primary_key=True)                 
    title = db.Column(db.String(120))                            
    content = db.Column(db.Text, nullable=False)                 
    date = db.Column(db.DateTime)                                
    sticky = db.Column(db.Boolean())                             
    username = db.Column(db.String(50), db.ForeignKey(User.name))
    image_one = db.Column(db.String(2083))                       
    image_two = db.Column(db.String(2083))                       
    image_three = db.Column(db.String(2083))                     
    image_four = db.Column(db.String(2083))                      
    tags = db.relationship('Tag', secondary=post_tags_table)     

And here's a screenshot of the admin view:

You can override the order like so (but PostModel and your own fields):

class TeamModel(AdminBase):    
    fields = ('name', 'competitions', 'image')
    search_fields = ['name']
    list_display = ('name', 'slug')

admin.register(models.Team, models.TeamModel)

AdminBase is just an auth mixin I have btw

# all admin views should subclass AuthMixin
class AuthMixin(object):
    def is_accessible(self):
        if current_user.is_authenticated() and current_user.has_role('Admin'):
            return True
        return False

class AdminBase(AuthMixin, model.ModelAdmin): # AuthMixin must come before ModelAdmin!
    """A base class for customizing admin views using our DB connection."""
    session = db.session

Thanks!