Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

How to create a registration flask form that affects two tables in order to link the a device to its owner

I’m developing a web application that visualizes sensor data so the users can monitor their devices. The web application uses Flask-Security and Flask-Admin to facilitate the user management and visualization.

The user data is stored in a user table.

class User(UserMixin, db.Model):
   id = db.Column(db.Integer, primary_key=True)
   first_name = db.Column(db.String(255))
   last_name = db.Column(db.String(255))
   email = db.Column(db.String(100), unique=True)
   password = db.Column(db.String(100))

The controller table has foreign key pointing to the user id in the user table.

class Controller(db.Model):
   id = db.Column(db.Integer, primary_key=True)
   controller_id = db.Column(db.String(100), nullable=False, unique=True)
   user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

The register template

<form action="{{ url_for_security('register') }}" method="POST" name="register_user_form">
   {{ register_user_form.hidden_tag() }}
   {{ render_field_with_errors(register_user_form.email) }}
   {{ render_field_with_errors(register_user_form.controller_id) }}
   {{ render_field_with_errors(register_user_form.first_name) }}
   {{ render_field_with_errors(register_user_form.last_name) }}
   {{ render_field_with_errors(register_user_form.password) }}
   {% if register_user_form.password_confirm %}
   {{ render_field_with_errors(register_user_form.password_confirm) }}
   {% endif %}
   {{ render_field(register_user_form.submit, class="btn btn-primary") }}
</form>

Looks like this on the page Register Form

The problem is that the user registration form doesn’t affect the controller table. I use extended validation form in order to add more fields.

class ExtendedRegisterForm(RegisterForm):
   first_name = StringField('First Name')
   last_name = StringField('Last Name')
   controller_id = StringField('Controller ID', [Required()], default="")

The new user has to be created first, and then the device can be linked to the user id, but the data is sent using the same form.

question from:https://stackoverflow.com/questions/65870118/how-to-create-a-registration-flask-form-that-affects-two-tables-in-order-to-link

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no magic here - you have to provide the data to your forms - Flask-Security doesn't do any magic query generation. In this case - I think you just need to implement the 'register' context processor and grab the controller id. Read more here: https://flask-security-too.readthedocs.io/en/stable/customizing.html#views


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...