Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom __view__ classes #91

Closed
timreinders opened this issue Aug 22, 2014 · 1 comment
Closed

Custom __view__ classes #91

timreinders opened this issue Aug 22, 2014 · 1 comment

Comments

@timreinders
Copy link

Hello,

Please be gentle, i'm a network admin, who's having a go 'programming' ;)

To the point.
When i specify a custom view class in my model, default form_create_rules inherited from 'flask.ext.admin.form.rules' seem to get overridden. As i now need to explicitly set a primary key, when
i create a new record, via the sandman admin interface.

Not specifying a custom 'view' results in the default behaviour, of not needing to specify a primary
key upon record creation.

I hope the i'm no being to cryptic.

my models as exposed to sandman

from sandman.model import register, activate, Model
from flask.ext.admin.contrib.sqla import ModelView
from sandman import db

class ViewVlan(View):
    column_list = ('name', 'tag', 'interface', 'ip', 'relatienr', 'comment')

class Vlan(Model):
    __tablename__ = 'vlan'
    __view__ = ViewVlan

    def __str__(self):
        return self.name

class Interface(Model):
    __tablename__ = 'interface'

    def __str__(self):
        return self.name

class Switch(Model):
    __tablename__ = 'switch'

    def __str__(self):
        return self.name

register((Vlan, Interface, Switch))
activate(admin=True, browser=False)

flask-sqlalchemy model

#!/usr/bin/env python

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite+pysqlite:///kiba.db'
app.config['DEBUG'] = True
db = SQLAlchemy(app)


class Switch(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.Unicode, unique=True)

class Interface(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.Unicode)
    vlan_id = db.Column(db.Integer, db.ForeignKey('vlan.id'))
    patch = db.Column(db.Text(1024))
    comment = db.Column(db.Text(1024))

class Vlan(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.Unicode)
    tag = db.Column(db.Integer)
    intefaces = db.relationship('Interface', backref='vlan', lazy='dynamic')
    ip = db.Column(db.Text(1024))
    comment = db.Column(db.Text(1024))
    relatienr = db.Column(db.Text(1024))

db.create_all()

Thanks,
Tim

@timreinders
Copy link
Author

The proposed change in #88 fixed my issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant