Hi friends all around the world ,There is a problem which is perplexing me for days .When I used Django I used obj=User.objects.filter(uname=username,pwd=password)
the User is a class to migrate to MySQL But when I used Flask-Sqlalchemy obj = User.query.filter(User.uname == username, User.pwd == password).first()
I need to do this to realize login. I think there is somethings different between Django and Flask. I know the "=" is assigning operator and the "==" is relational operator. It's appreciate that you can help me , Thank you !
-
Its not `Flask` that does that, It's `SQL-Alchemy`, And the other one is not Django, That's `Django ORM` – DarkSuniuM Oct 08 '18 at 03:03
-
1@DarkSuniuM While I agree on the first part, the second is misleading since Django ORM is a part Django. – Klaus D. Oct 08 '18 at 03:17
-
@KlausD. Its a single app, You can use it standalone, You can remove the line that adds `Django ORM` to `django` in `settings.py` of your project, Its belongs to django, but it's not django itself! – DarkSuniuM Oct 08 '18 at 03:19
-
Sounds like: No, he was not hit by a car. He was only hit by *the front* of a car. – Klaus D. Oct 08 '18 at 03:21
-
@KlausD. You can't use a car that does not have a front, You can use Django without it's ORM, Actually you can use Django with SQL-Alchemy or even you can execute raw SQL queries in Django! it's better to be more specific about what we talking about! – DarkSuniuM Oct 08 '18 at 03:25
-
Being precise is good, being more precise than necessary is inefficient. – Klaus D. Oct 08 '18 at 03:33
-
1@DarkSuniuM while some of your point is valid, it's simply not true that you can "remove the line that adds Django ORM in settings.py". Are you thinking of the auth framework, perhaps? And you *definitely* can't use Django ORM "standalone". – Daniel Roseman Oct 08 '18 at 06:43
1 Answers
First of all, it's not Flask
or Django
that does such a thing!
The one that you used with Flask
is SQL-Alchemy
and The one that you used with Django is Django ORM
, You can use them standalone without a Flask
or Django
Project!
On both snippets, You are assigning an object to a variable!
obj = User.objects.filter(uname=username,pwd=password)
is equal to MySQL query
SELECT *
FROM users
WHERE uname = <username> AND pwd = <password>;
while
obj = User.query.filter(User.uname == username, User.pwd == password).first()
is equal to MySQL query
SELECT *
FROM users
WHERE uname = <username> AND pwd = <password>
LIMIT BY 1;
So the Django ORM
one will return an iterable of rows that met our condition!
You can use all()
instead of first()
on SQL-Alchemy
and get the same result like Django ORM
!
There is a method called filter_by()
on SQL-Alchemy
which seems more like your Django ORM
example, It still gets only one or 404 if you use first()
at the end, But you can set condition without condition operators but by assigning them on method parameters, like:
obj = User.query.filter_by(uname=username, pwd=password).first()
Check this StackOverflow Question for more information about filter
vs filter_by
on SQL-Alchemy

- 2,523
- 2
- 26
- 43
-
I tried and It works ! Thank you for your help and I will learn filter_by() with your link .Actually I learned Django first and now I start to learn Flask ,I think I'm a little confused.. – Vasquez Oct 08 '18 at 03:30
-
@Vasquez Don't compare them, so you won't get confused, Btw Accept the answer and vote up if it helped ;-) – DarkSuniuM Oct 08 '18 at 03:32
-
Ok, I have voted it up . You helped me a lot .Thank you for your advice and I will try to learn Flask with you advice. – Vasquez Oct 08 '18 at 03:44