[Flask-SQLAlchemy] Querying association table in flask-sqlalchemy?
Let's say I have two models **Posts** and **Tags** and an association table **post_tags** that connects these models in a many-to-many relationship (one post can have one or many tags and each tag can have one or more posts attached to it):
post_tags = db.Table(
'post_tags',
db.Column('post_id', db.Integer, db.ForeignKey('posts.id'), primary_key=True),
db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'), primary_key=True),
)
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text, nullable=False)
created_at = db.Column(db.Integer, nullable=False)
class Tag(db.Model):
__tablename__ = 'tags'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True, nullable=False)
I want to obtain the most frequently mentioned tags from the **post_tags** table. SQLite query for this roughly looks like:
SELECT tag_id, count(tag_id) FROM post_tags GROUP BY tag_id ORDER BY count(tag_id) DESC;
How can I accomplish this in SQLAlchemy? I am using Flask-SQLAlchemy extension and SQLite as a DB driver.
Edit: formatting and grammar
/r/flask
https://redd.it/8idv32
Let's say I have two models **Posts** and **Tags** and an association table **post_tags** that connects these models in a many-to-many relationship (one post can have one or many tags and each tag can have one or more posts attached to it):
post_tags = db.Table(
'post_tags',
db.Column('post_id', db.Integer, db.ForeignKey('posts.id'), primary_key=True),
db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'), primary_key=True),
)
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text, nullable=False)
created_at = db.Column(db.Integer, nullable=False)
class Tag(db.Model):
__tablename__ = 'tags'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True, nullable=False)
I want to obtain the most frequently mentioned tags from the **post_tags** table. SQLite query for this roughly looks like:
SELECT tag_id, count(tag_id) FROM post_tags GROUP BY tag_id ORDER BY count(tag_id) DESC;
How can I accomplish this in SQLAlchemy? I am using Flask-SQLAlchemy extension and SQLite as a DB driver.
Edit: formatting and grammar
/r/flask
https://redd.it/8idv32
reddit
r/flask - [Flask-SQLAlchemy] Querying association table in flask-sqlalchemy?
4 votes and 1 so far on reddit
Can I host Django website where i currently host my Wordpress website?
I have a hosting service for 1 year but I use it to host Wordpress.Can i host my Django website on the server instead of the Wordpress?
What are the requirements to host Django?
/r/django
https://redd.it/8itn2j
I have a hosting service for 1 year but I use it to host Wordpress.Can i host my Django website on the server instead of the Wordpress?
What are the requirements to host Django?
/r/django
https://redd.it/8itn2j
reddit
Can I host Django website where i currently host my... • r/django
I have a hosting service for 1 year but I use it to host Wordpress.Can i host my Django website on the server instead of the Wordpress? What are...
[AF] Pagination not working, has_next and has_prev always gives None
Hi, so as the title suggests I have a problem regarding pagination. Here is the code I have tried to use (taken from [Miguel Grinberg's tutorial](https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ix-pagination)).
def index():
"""View function for the index site, basically the main site.
Sorts posts by hotness"""
page = request.args.get('page', 1, type=int)
posts = Post.query.order_by(Post.hotness.desc()).paginate(
page, app.config['POSTS_PER_PAGE'], False)
next_url = url_for('index', page=posts.next_num) \
if posts.has_next else None
prev_url = url_for('index', page=posts.prev_num) \
if posts.has_prev else None
return render_template('index.html', title='Dopenet: You can do anything', posts=posts.items, next_url=next_url, prev_url=prev_url)
The index.html file is this:
{% extends "base.html" %}
{% block content %}
{% for post in posts if posts %}
{% include '_post.html' %}
{% endfor %}
{% if prev_url %}
<a href="{{ prev_url }}">Newer posts</a>
{% endif %}
{% if next_url %}
<a href="{{ next_url }}">Older posts</a>
{% endif %}
{% endblock %}
So I am guessing that both has_next and has_prev are giving `None` as a response, so hence no url for both prev and next.
What am I doing wrong and how can I fix this?
The rest of the code can be found here, if more context is needed:
https://www.github.com/ModoUnreal/Nuncio
/r/flask
https://redd.it/8ixivl
Hi, so as the title suggests I have a problem regarding pagination. Here is the code I have tried to use (taken from [Miguel Grinberg's tutorial](https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ix-pagination)).
def index():
"""View function for the index site, basically the main site.
Sorts posts by hotness"""
page = request.args.get('page', 1, type=int)
posts = Post.query.order_by(Post.hotness.desc()).paginate(
page, app.config['POSTS_PER_PAGE'], False)
next_url = url_for('index', page=posts.next_num) \
if posts.has_next else None
prev_url = url_for('index', page=posts.prev_num) \
if posts.has_prev else None
return render_template('index.html', title='Dopenet: You can do anything', posts=posts.items, next_url=next_url, prev_url=prev_url)
The index.html file is this:
{% extends "base.html" %}
{% block content %}
{% for post in posts if posts %}
{% include '_post.html' %}
{% endfor %}
{% if prev_url %}
<a href="{{ prev_url }}">Newer posts</a>
{% endif %}
{% if next_url %}
<a href="{{ next_url }}">Older posts</a>
{% endif %}
{% endblock %}
So I am guessing that both has_next and has_prev are giving `None` as a response, so hence no url for both prev and next.
What am I doing wrong and how can I fix this?
The rest of the code can be found here, if more context is needed:
https://www.github.com/ModoUnreal/Nuncio
/r/flask
https://redd.it/8ixivl
GitHub
ModoUnreal/nuncio
nuncio - The fair news website
How to redirect a request on api point to a docker container?
I’m want requests sent on a route to be redirected to that of a container (exposed via ports)
/r/flask
https://redd.it/8dxkpc
I’m want requests sent on a route to be redirected to that of a container (exposed via ports)
/r/flask
https://redd.it/8dxkpc
reddit
How to redirect a request on api point to a docker... • r/flask
I’m want requests sent on a route to be redirected to that of a container (exposed via ports)
A month ago /u/AlSweigart asked for help reviewing his book. I delivered and so did he.
/r/Python
https://redd.it/8ixbcm
/r/Python
https://redd.it/8ixbcm
[Video] Django/Vue RSS reader with lesson document & timestamped links. Thanks /r/django for helping make this happen!
https://youtu.be/0FTaWat_VsM
/r/django
https://redd.it/8iwqu4
https://youtu.be/0FTaWat_VsM
/r/django
https://redd.it/8iwqu4
YouTube
Let's Make an RSS Reader (Django and Vue)
Let's Make an RSS Reader with Django & Vue.
Lesson
HTML: https://blog.bastions.co/lessons/lets-make-an-rss-reader.html
Word doc: https://blog.bastions.co/lessons/docs/lets-make-an-rss-reader.docx
Timestamps
1: Initial setup: https://youtu.be/0FTaWat_VsM?t=65…
Lesson
HTML: https://blog.bastions.co/lessons/lets-make-an-rss-reader.html
Word doc: https://blog.bastions.co/lessons/docs/lets-make-an-rss-reader.docx
Timestamps
1: Initial setup: https://youtu.be/0FTaWat_VsM?t=65…
Jupyter Map - A map with all the institutions using Jupyter
https://elc.github.io/jupyter-map/
/r/IPython
https://redd.it/8ixvf7
https://elc.github.io/jupyter-map/
/r/IPython
https://redd.it/8ixvf7
reddit
Jupyter Map - A map with all the institutions using... • r/IPython
6 points and 1 comments so far on reddit
2D CAD/vectors drawing library
Hey, I'm looking to draw some patterns in Python based on a set of inputs. Need to do things like lines, tangents, and arcs. Does anyone know of library I could use to draw on a canvas?
/r/Python
https://redd.it/8j0vr2
Hey, I'm looking to draw some patterns in Python based on a set of inputs. Need to do things like lines, tangents, and arcs. Does anyone know of library I could use to draw on a canvas?
/r/Python
https://redd.it/8j0vr2
reddit
2D CAD/vectors drawing library • r/Python
Hey, I'm looking to draw some patterns in Python based on a set of inputs. Need to do things like lines, tangents, and arcs. Does anyone know of...
Boilerplate code for using Django and React Already configured
https://github.com/idanwekhai/Django_React_boilerplate
/r/django
https://redd.it/8iznt6
https://github.com/idanwekhai/Django_React_boilerplate
/r/django
https://redd.it/8iznt6
GitHub
idanwekhai/Django_React_boilerplate
Django_React_boilerplate - Boiler-Plate code to Use Django with React
help to create a custom user form
Hi I would like to know how I can customize my user model, I haves this model:
from django.db import models
from django.contrib.auth.models import AbstractUser, UserManager
class CustomUserManager\(UserManager\):
pass
class CustomUser\(AbstractUser\):
objects = CustomUserManager\(\)
and I have this form
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm\(UserCreationForm\):
class Meta\(UserCreationForm.Meta\):
model = CustomUser
fields = \('username', 'email'\)
class CustomUserChangeForm\(UserChangeForm\):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
I need to know how I can add more fields to the model and how implement it in the form for example I want to add ID, adrees and Bday fields
/r/djangolearning
https://redd.it/8j1htl
Hi I would like to know how I can customize my user model, I haves this model:
from django.db import models
from django.contrib.auth.models import AbstractUser, UserManager
class CustomUserManager\(UserManager\):
pass
class CustomUser\(AbstractUser\):
objects = CustomUserManager\(\)
and I have this form
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm\(UserCreationForm\):
class Meta\(UserCreationForm.Meta\):
model = CustomUser
fields = \('username', 'email'\)
class CustomUserChangeForm\(UserChangeForm\):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
I need to know how I can add more fields to the model and how implement it in the form for example I want to add ID, adrees and Bday fields
/r/djangolearning
https://redd.it/8j1htl
reddit
help to create a custom user form • r/djangolearning
Hi I would like to know how I can customize my user model, I haves this model: from django.db import models from django.contrib.auth.models...
I'm working on this free tool for practicing Python (it's like Duolingo for learning to code)
https://edabit.com/explore?lang=python3
/r/Python
https://redd.it/8j3pnn
https://edabit.com/explore?lang=python3
/r/Python
https://redd.it/8j3pnn
edabit
Explore 675+ FREE Coding Challenges // Edabit
Edabit is like Duolingo for learning to code. Earn points for correct solutions, unlock achievements and level up. Our coding challenges make learning to code fun and easy.
[P] Introducing Deep Conversation feature of Dragonfire open-source virtual assistant
https://github.com/DragonComputer/Dragonfire#deep-conversation-examples
/r/MachineLearning
https://redd.it/8j3f1j
https://github.com/DragonComputer/Dragonfire#deep-conversation-examples
/r/MachineLearning
https://redd.it/8j3f1j
GitHub
GitHub - DragonComputer/Dragonfire: the open-source virtual assistant for Ubuntu based Linux distributions
the open-source virtual assistant for Ubuntu based Linux distributions - DragonComputer/Dragonfire
[Project] Tensorflow implementation of Generative Adversarial Networks for Extreme Learned Image Compression
https://github.com/Justin-Tan/generative-compression
/r/MachineLearning
https://redd.it/8j44cd
https://github.com/Justin-Tan/generative-compression
/r/MachineLearning
https://redd.it/8j44cd
GitHub
GitHub - Justin-Tan/generative-compression: TensorFlow Implementation of Generative Adversarial Networks for Extreme Learned Image…
TensorFlow Implementation of Generative Adversarial Networks for Extreme Learned Image Compression - Justin-Tan/generative-compression
[D] A good question I stumbled upon: Why doesn't regularization solve Deep Neural Nets hunger for data?
https://stats.stackexchange.com/questions/345737/why-doesnt-regularization-solve-deep-neural-nets-hunger-for-data
/r/MachineLearning
https://redd.it/8izegs
https://stats.stackexchange.com/questions/345737/why-doesnt-regularization-solve-deep-neural-nets-hunger-for-data
/r/MachineLearning
https://redd.it/8izegs
Cross Validated
Why doesn't regularization solve Deep Neural Nets hunger for data?
An issue I've seen frequently brought up in the context of Neural Networks in general, and Deep Neural Networks in particular, is that they're "data hungry" - that is they don't perform well unless...
PyCon 2018 Talk Videos
https://www.youtube.com/channel/UCsX05-2sVSH7Nx3zuk3NYuQ/featured
/r/Python
https://redd.it/8j4ep6
https://www.youtube.com/channel/UCsX05-2sVSH7Nx3zuk3NYuQ/featured
/r/Python
https://redd.it/8j4ep6
[AF] Updated website is causing problem for old users, possible cookie issues
To remove "server/main.cgi/" from example.com/server/main.cgi/mypage I changed
@app.route('/mypage/')
to
@app.route('/mypage')
But now, when my old users click the button to open /mypage, the url "example.com/server/main.cgi/mypage/" is requested, which is no longer used. Altough the button url says example.com/mypage.
I am not sure if this is caused by old cookies or cache, but there are no issues when i open the website in a private window.
Also, I am sure there must be some better way to remove that part of the url, because now, /mypage/ wont work.
My .htaccess file looks like this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home/www/server/main.cgi/$1 [L]
main.cgi
#!/usr/bin/python3
import sys
sys.path.insert(0, '/home/.local/lib/python3.4/site-packages')
from wsgiref.handlers import CGIHandler
from myapp import app
CGIHandler().run(app)
myapp.py
from flask import Flask, render_template
app = Flask(__name__, static_folder="../static",
template_folder="../templates")
@app.route('/mypage')
def mypage():
return render_template('mypage.html')
if __name__ == '__main__':
app.run()
I appreciate any help I can get on this issue.
Edit: All my imports are listed with for example "/server/main.cgi/static/js/bootstrap.bundle.js". Is it possible to change it to "/static/js/bootstrap.bundle.js"?
/r/flask
https://redd.it/8j349j
To remove "server/main.cgi/" from example.com/server/main.cgi/mypage I changed
@app.route('/mypage/')
to
@app.route('/mypage')
But now, when my old users click the button to open /mypage, the url "example.com/server/main.cgi/mypage/" is requested, which is no longer used. Altough the button url says example.com/mypage.
I am not sure if this is caused by old cookies or cache, but there are no issues when i open the website in a private window.
Also, I am sure there must be some better way to remove that part of the url, because now, /mypage/ wont work.
My .htaccess file looks like this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home/www/server/main.cgi/$1 [L]
main.cgi
#!/usr/bin/python3
import sys
sys.path.insert(0, '/home/.local/lib/python3.4/site-packages')
from wsgiref.handlers import CGIHandler
from myapp import app
CGIHandler().run(app)
myapp.py
from flask import Flask, render_template
app = Flask(__name__, static_folder="../static",
template_folder="../templates")
@app.route('/mypage')
def mypage():
return render_template('mypage.html')
if __name__ == '__main__':
app.run()
I appreciate any help I can get on this issue.
Edit: All my imports are listed with for example "/server/main.cgi/static/js/bootstrap.bundle.js". Is it possible to change it to "/static/js/bootstrap.bundle.js"?
/r/flask
https://redd.it/8j349j
linkedin/shiv: Shiv is a command line utility for building fully self contained Python zipapps as outlined in PEP 441, but with all their dependencies included.
https://github.com/linkedin/shiv
/r/Python
https://redd.it/8j5xn8
https://github.com/linkedin/shiv
/r/Python
https://redd.it/8j5xn8
GitHub
GitHub - linkedin/shiv: shiv is a command line utility for building fully self contained Python zipapps as outlined in PEP 441…
shiv is a command line utility for building fully self contained Python zipapps as outlined in PEP 441, but with all their dependencies included. - linkedin/shiv
Should I use cron or celery for a task to be performed at the exact same time twice a month?
I need to perform a specific task at exactly 12:00 AM on the 1st and 16th day of the month. The task is pretty simple; it is setting a model IntegerField to 0.
I know the 2 major apps for this are cron and celery. I'm not experienced at all with either of them. Which one of them is optimal for my situation?
/r/django
https://redd.it/8j903y
I need to perform a specific task at exactly 12:00 AM on the 1st and 16th day of the month. The task is pretty simple; it is setting a model IntegerField to 0.
I know the 2 major apps for this are cron and celery. I'm not experienced at all with either of them. Which one of them is optimal for my situation?
/r/django
https://redd.it/8j903y
reddit
r/django - Should I use cron or celery for a task to be performed at the exact same time twice a month?
4 votes and 6 so far on reddit