Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
"Cannot add foreign key constraint" when creating test database for tests.

I have a database that works fine, I can interact with it normally through Django. When I try to run unit tests, I use the following command:

python3 manage.py test --settings appname.settings_test

But I get the following error when creating the database:

django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')

One of my tables does indeed have a foreign key relationship with another, so I'm assuming that the table with the foreign key field is being added before the table that it depends on.

How can I verify that this is the case, and how can I go about addressing this issue so that tables are executed in the correct order?

I've tried a few different fixes, such as re-ordering the apps with each model in the INSTALLED_APPS setting, but nothing I've tried has worked. Any help would be appreciated! I am happy to provide any other information!

EDIT: Full stack trace below. Note that I am deleting the old database at the beginning because I am running this command multiple times in a row, so it's deleting the failed database from my last attempt.

Creating test database for alias 'example'...
Got an error creating the test database: (1007, "Can't create database 'test_example'; database exists")
Type 'yes' if you would like to try deleting the test database 'test_example', or 'no' to cancel: yes
Destroying old test database for alias 'example'...
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/cursors.py", line 374, in _do_query
db.query(q)
File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py", line 277, in query
_mysql.connection.query(self, query)
_mysql_exceptions.IntegrityError: (1215, 'Cannot add foreign key constraint')

/r/django
https://redd.it/8dofs3
Plot real-time sensor data on a map

I have real-time sensor gps-data (comes from a Kafka consumer) and want to live-track them on a map. Can folium / jupyter-gmaps do that without permanently updating the map in the background? Ideally can I use ipywidgets with those libraries too to build interactive maps?

/r/IPython
https://redd.it/8dozey
Need help for a RSS feeder app

I'm very new to django and I want to build an RSS and screen-scraping app that will automatically check the feed using crontab and puts the feed in my database. Later I want to show the feed on the website. I know there are few pre build apps like planet but they are outdated and I wanna make it in django 2.0. Am I going in a right direction? Please feel free to leave any suggestion you have

/r/django
https://redd.it/8dn7hv
Validator-Collection / What's your validator wishlist?

Hi Folks!

We just released a [Validator-Collection](https://github.com/insightindustry/validator-collection) library, where v.1.0 supports 60+ of the most common validation approaches we tend to use "in the wild". Here's direct link: https://github.com/insightindustry/validator-collection

We built the collection with the validations that we find ourselves using most often in our projects, so it's got all of the usual suspects (email, URLs, UUID, etc.). But now that we've released v.1.0 of the library, I'm hoping to find out: What kinds of validation do you use most often?

We're thinking of extending the library with image validation (type, size, etc.), configurable password pattern validation, and the like. We'd probably implement them using an extras-requires pattern (that way users of the library who don't want to install heavy image dependencies don't have to).

So I've got two questions for folks:

1. What kinds of validations do you find yourself using most often? What kinds of validations would you find most helpful?

2. Philosophically, how do you feel about a library that uses the extras-require pattern for dependency management?

Meaning, typical installation (no dependencies outside of the standard library, unless on Python 2.7):

pip install validator-collection

(does not include image validation support)

But if you DO want image support, installation using:

pip install validator-collection[images]

Thanks - I look forward to hearing (reading) your thoughts!

(also, FWIW: I'm sensitive to the perennial debate about LBYL and EAFP - philosophically, we've designed the library's API to support both approaches...with luck, we've done a good job balancing between the two).

All the best,
Chris

/r/Python
https://redd.it/8dp4g3
Flask-YoloAPI 0.1.3 released

I released Flask-YoloAPI version `0.1.3`.

https://github.com/skftn/flask-yoloapi

It allows you to create Flask routes with refined requirements, such as;

- quickly create safe routes by enforcing input/type validation
- coercing of data types
- support for many datatypes
- understands docstrings
- support for python 3.5 type annotations
- custom validation via the `validator=` flag

This library embodies the philosophy of Flask; 'micro web-development'. Quick and easy route creation where you don't need many bells and whistles, yet, you can be very descriptive and strict about what comes into your route.

It is already used in production for the past 6 months under heavy load and works fine. It features a test-suite with 90%+ coverage.

Suggestions welcome, as always.

/r/flask
https://redd.it/8dmgfx
How to handle JWT authentication in a RESTful API with flask-praetorian (mainly logging users out and handling errors)?

Hi,

I am using [flask\-praetorian](http://flask-praetorian.readthedocs.io/en/latest/index.html), it works fine for logging a user in and protecting a route, but beyond that, I have no idea how to use it to accomplish other tasks like logging out users, handling errors...

For example, accessing a protected route results in the following exception \(with a `500` error instead of a` 40`1 error as it's supposed to\):

flask_praetorian.exceptions.MissingTokenHeader: JWT token not found in headers under 'Authorization' (401)

I am sure other errors \(like an invalid token\) have the same behavior. How can I return a custom message with the proper status code?

Also, I don't know much about JWT, I have read that keeping the user logged in with the same token for an extended period of time is a bad idea, so how can I allow logging out and token expiration? Do you guys have any resources I need to read before making an API with weak security?

Thanks!

/r/flask
https://redd.it/8dqbhl
Django Rest Framework + Vue.js - Live notifications like on the FB

I have a social network project written in Django Rest Framework + Vue.js. I need to add new feature - live notifications like on the Facebook. For example:

* user A commented your post

* user K started fallowing you


and much more actions.

What is the best way to do this? Any suggestions are very welcome.

/r/django
https://redd.it/8dqbz0
Creating a new molecule in ASE.

Been doing some searching on the internet and haven't found a good tutorial for my question:

I'm trying to model https://en.wikipedia.org/wiki/HMX using ASE try to optimize its geometry and it obviously isn't in the ase.collections.g2 database.

Can anyone point me in the right direction on where to get started making new molecules not in the ASE database?

Thank you and I apologize if this is in the incorrect subreddit.

/r/Python
https://redd.it/8dsgs6
Going from Matlab to Python?

I was wanting to hear some thoughts on how easy the transition is from Matlab to Python. I’d say I’m becoming sorta proficient in Matlab at this point, am I gonna have an easy time picking up Python with that background? I’m looking to mainly use it for stuff you’d see in engineering school like solving math problems and all that.

/r/Python
https://redd.it/8dtj7k
How do I move my Django project to Linode?

I have a Django project that works fine on my local machine. The project hooks up to a Postgres database. I want to move it to a Linode Debian instance. But I cannot find clear instructions on how to move my project to the Linode server. Anyone have experience moving a Django project to Linode? Got some pointers?

/r/django
https://redd.it/8dtbim
Minesweeper for command line [Linux]

/r/Python
https://redd.it/8dxorh
Issue: Text deletes when i type in front of it.

This just started happening a few days ago and I have never had this issue before. When I start typing code in front of a previous line of code it deletes it. For example, if a line has "print('Hello World!')" and I use my cursor to click in front of the word "print" and type "read", then it would be "readt('Hello World!')". Does anyone know how to fix this? Any help would be greatly appreciated.

/r/IPython
https://redd.it/8dxepo
A handy cheatsheet for Python
http://pysnippets.com

/r/Python
https://redd.it/8e1mzq
User Session and Session Data

Is there any way to store session data based on a User's session?

i.e. `request.user['foo'] = 'bar'`

I know the above code doesn't work, so I was wondering if there is something built in where you could individually store data in sessions with each user? Or is there another to do something like this, because I need to use data across different views and I need to keep it separate from each User.

/r/djangolearning
https://redd.it/8e1v8q
Impressed!

Fastest web development framework I have encountered till now.
Made a website in 24hours, which would have otherwise taken at least a week.
Thanks Flask!

/r/flask
https://redd.it/8e2o6e