1

I am having issues with a seemingly simple sqlalchemy query using Flask.

I have a table called Links and within that table there are columns called 'id', 'author_id', 'link', and 'group'. My models.py looks like this:

class Links(db.Model):

__tablename__='links'

id = db.Column(db.Integer, primary_key=True)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
link = db.Column(db.String, unique=False, nullable=True)
group = db.Column(db.String, unique=False, nullable=False)


def __init__(self, author_id=None, link=None, group=None):
    self.author_id = author_id
    self.link = link
    self.group = group

def __repr__(self):
    return'<Link %r>' %(self.link)

I would like to return the values of all groups associated with the user that is logged into the application. Here is my views.py file:

 @app.route('/members/', methods=['GET','POST'])
@login_required
def members():
    error=None
    form = PostLink(request.form, csrf_enabled = False)
    uid = session['user_id']
    link = "NULL"
    groups = Links.query.filter_by(author_id=uid).all()

    if request.method=='POST':
        if form.validate_on_submit():
            new_group = Links(
                       uid,
                       form.group.data,
                       link,
                       )
            try:
                   db.session.add(new_group)
                   db.session.commit()
                   flash("You have created a group!")
            except IntegrityError:
                    error = 'That group did not work, maybe it already exists?'
            else:
             flash_errors(form)
    return render_template('members.html', form=form, error=error, link = link, groups=groups)

And my 'members.html':

{% extends "base.html" %}

{% block content %}

  <p>Add New Group: {{ form.group }}</p>
  <input id="link" type="hidden" name="link" value= {{ link }}/>
  <p><input type="submit" value="Request"></p>
</form>
<br/>
{% for group in groups %}
<li><p>
{{ group }}
</p></li>
{% endfor %}

{% endblock %}

Currently this is just returning a list of links and groups in an odd format:

<Link u'link2'>

<Link u'linky'>

<Link u'linkymaybe'>

<Link u'madeit'>

<Link u'BLAH'>

So the core of my question is how do I build a query using SQLAlchemy to display all groups associated with the logged in user (uid = session['user_id']) I am pretty new to Flask and this problem is becoming an issue as I have tried a number of filter_by and filter statements with no luck at all.

Thank you in advance!

2 Answers 2

2

It is displaying correctly the object "Link" returned by the query. You need to format it in the template.

Thisi is link {{ group.link }} from author #{{ group.author_id }} in group named {{ group.group }}

Maybe you've chosen a bad name "group" when cycling on results in the template. It should be called link.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked great. One more question. How can I filter the output to show no duplicate groups?
0

In the template, you can show the group name using {{ link.group }} instead of {{ link }}. The query is returning the whole object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.