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
Admin page not displaying list_display

I'm working through MDN's tutorial on Django and I'm up to the Admin page stuff.

I am trying to register my Author model in my admin pages like so:

from django.contrib import admin
from .models import Author

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = ('last_name', 'first_name', 'date_of_birth', 'date_of_death')

When I go to `localhost:8000/admin/catalog/author` I'm still seeing the `__str__()` representation of the model. What am I getting wrong?

/r/djangolearning
https://redd.it/b6ozi7
Django app getting pinged by bots looking for vulnerabilities--what should I be doing in response for security(site on Heroku behind Cloudflare)?

Hey all, have a django site running on heroku behind cloudflare and noticed in the logs that site is often getting pinged by bots looking for vulnerabilities (ie. queries to mysite.com/wp-login or mysite.com/com/fileuploader etc.). It's not that frequent that it's a DDOS, these all show 404 error codes so I think they just don't get through, and most of the pings are for wordpress sites anyway.

I'm wondering if I should be doing anything to prevent these attacks or ban these ips? I am on the Cloudflare plan with the WAF. Cloudflare has an option in the WAF to ban certain IPs, should I be adding these IPs to that list(any way to automate this?) or something else? Just wondering if there's a best practice here. Thank you all.

/r/django
https://redd.it/b6p8nu
defer and some concurrency [AF]

Hey everyone,

I've got a request that does a large amount of insertions in a psql db. This takes quite some time and results in the request/response cycle returning a tiimeout. Someone a lot more knowledgable than I'll ever be suggested flask_defer but for the life of me I can't get it to provide the response back to the client prior to inserting the data into the database. I've seen the likes of celery and python-rq. Although these seem scary and like way too much to simply have a task running in the background with me being the sole client to access this site.

So just to lay out the problem better. Every evening I'll automatically start submitting data to the database. The insertion of the data takes quite some time and thus the response isn't returned in a timely manner. The site will only be used by me, so it won't be receiving a lot of attention. I want it to be able to return the response prior to doing the database insertions. Some level of concurrency will be needed as when flask returns the response, I'll immediately start another request, to insert another set of data, and if the prior

/r/flask
https://redd.it/b45634
Getting data from dynamically created inputs

I have some code where I can dynamically add inputs via javascript code. But I'm unable to figure out how to get the data from the input boxes created. How can I get the data back into my python scripts? Do I have to do this with JS?

EDIT: So I found a solution.

In my forms.py I utilize a HiddenField(). Then in my javascript I take my addCount var used in generating the input boxes and store that value into my HiddenField. Then from my routes.py I am able to access the HiddenField data via request.form.get('hiddenNum'). Once I have that value, I can loop over the range to generate the names for each added input box.

Forms:

class SubDirForm(FlaskForm):
subDirName = TextField(validators=[DataRequired()])

class SubDirsForm(FlaskForm):
subDirList = FieldList(FormField(SubDirForm), min_entries=0)
submit = SubmitField(label='Print')
addField = ButtonField(label='Add Field', id='addNewField')
hiddenNum = HiddenField()



Route:

@bp.route('/', methods=['GET', 'POST'])


/r/flask
https://redd.it/b3tkb1
[P] I scraped 32,000 cars, including the price and 115 specifications

NEW cars. Specs include MSRP, Gas Mileage, Engine, EPA Class, Style Name, Drivetrain, SAE Net Torque @ RPM, Fuel System, Engine Type, SAE Net Horsepower @ RPM, Displacement, etc. The cars are all cars available on the American market.

Get the CSV (44MB) on my [drive](https://drive.google.com/file/d/1HnpfG2xj_6EZ7Tgle2QB9Yn_YS7r7uVA/view?usp=sharing), or look how I did it on [Github](https://github.com/nicolas-gervais/predicting-car-price-from-scraped-data/blob/master/scraping).


/r/MachineLearning
https://redd.it/b6r34a
[AF]Tag input form in Flask?

I've been trying to implement what is really just a form where you type a word, hit a comma, and the word is put into a little container with an 'X' next to it, pretty much what you would expect from a form that is looking to separate tags.

Does anyone know the name of what this box is called or point me to a form/library that does this? It has been difficult Googling help for this as I don't seem to be coming on the right name for it.

/r/flask
https://redd.it/b3ss4k
Added Docker support for dev environment

Hi guys,

previously is shared my boilerplate for flask, now i am here with another change that is docker based development environment. I added the docker support for the dev environment.

Here is the link to the repo:

[https://github.com/a-wakeel/flask-bp](https://github.com/a-wakeel/flask-bp)

​

Will appreciate any suggestions and appreciations.

Cheers !

/r/flask
https://redd.it/b3n2i2
If you were to create only the frontend part of an eCommerce site in 2019, is it a terrible idea to use Django?

I'm looking at using Saleor for the backend of an eCommerce and making use of the GraphQL API to build a "core" backend that can be replicated over a number of shops with the database being the only variable, and a number of frontends for different shops for each of the brands that we sell.

Now I'm not much of a javascript guy (outside of some basic jQuery stuff, the "modern" ES6 syntax makes me feel a little sick) so I really do not understand the Saleor React based [storefront frontend](https://github.com/mirumee/saleor-storefront).

​

So I'm left with a few options:

1. Suck it up and try and learn their complicated React frontend
2. Build a frontend in Django and use that to talk to the backend, use Django templates and a spinkling of jQuery as I have done in the past.
3. Build a new frontend in React or Vue and learn how it all works for myself

​

The requirements any front end would meet would be:

* Any easy to understand templating system, ideally something similar to [Shopify's liquid templates](https://shopify.github.io/liquid/basics/introduction/) or [EKM Tags](https://tags.ekm.com/)
* Full server side rendering, SEO is very important to us

​

I'm not looking to build fancy SPA's or PWA's, just simple, performant shops based on

/r/django
https://redd.it/b6vewm
I used the Spotify lyrics script uploaded here last week to create a Telegram bot. It sends you the lyrics of the song you're currently listening, so you can get them on the go! Running as Chatot bot (@Spoti_lyrics_bot) on Telegram.

[https://github.com/manuhortet/spotify-lyrics-bot](https://github.com/manuhortet/spotify-lyrics-bot)

/r/Python
https://redd.it/b6vx0i
Nullable PositiveIntegerField to non-nullable CharField with default w/ Postgres

I am having an issue with my migrations. I am trying to change nullable PositiveIntegerField into non-nullable CharField with a default value. Said migration works like a charm if I don't have any null values in the db, it crashes however with `django.db.utils.OperationalError: cannot ALTER TABLE because it has pending trigger events` error when it encounters a null. I have a workaround that first changes the int field to nullable char field and maps null values to empty string. I would then make yet another migration that would make the field not-nullable. However, I would like to do it all in a single migration. Any ideas or suggestions?


/r/django
https://redd.it/b6yu2l
My largest python program that i wrote after learning python. It's nothing much compared to what you guys post here.

/r/Python
https://redd.it/b6yegi
Flask-login + Redis

Hi Everyone,


I'm developing my first flask app. I'm already using Redis for some other function and I'm now trying to add some security via user logins.

I'm wondering if its possible and if its safe to use Redis to manage my users for Flask Login

​

Thank you!

/r/flask
https://redd.it/b6yf7o
Using JSONField to extend models in an asset inventory - good idea?

(Originally posted in /r/djangolearning but didn't get much traction. Hoping to get more input/ideas/best practices)

I'm building an asset management system as part of a larger project. I want to allow end users to add additional fields for assets.

I have three levels of models that help to organize and describe assets: AssetType in the most generic, then Asset, and finally the individual AssetInstance - the record for a single item.

Here are some stripped down model definitions:

class AssetType(models.Model):
'''
Very broad model used to create a hierarchical categorization of assets. For instance, "Vehicle"
'''

asset_type = ForeignKey(AssetType, ) # Asset Types can belong under other Asset Types
title = models.CharField(max_length=100)

additional_fields = models.JSONField() # Additional fields and parameters to be added to all AssetInstances of this AssetType


class Asset(models.Model):
'''
More specific . For instance, "Truck" with

/r/django
https://redd.it/b759rf
Performance issues on OSX, Python 3.7

I am facing a problem of having incredibly slow startups of basic things like running unit tests on OSX. On Ubuntu, commands like *pip list* instantly return packages installed in conda environment but same command on OSX takes 25 seconds to show up.

Similar behavior is exhibited when running unit tests for any amount of tests, for example test_X.py using something like *python -m pytest ./tests/unit/test_X.py immediately starts on Ubuntu but takes 15 seconds to load *something* on a Mac.

Anyone faced such problems? I am especially interested in benchmarking tools that can show me a granular view of the bottlenecks.

================================================ test session starts =================================================
platform darwin -- Python 3.6.6, pytest-3.10.1, py-1.7.0, pluggy-0.8.1 -- /Users/user_x/miniconda3/envs/bmlldev/bin/python
cachedir: .pytest_cache
Using --randomly-seed=1553934664
rootdir: /Users/user_x/work/project, inifile: setup.cfg
plugins: randomly-1.2.3, profiling-1.4.0, mock-1.10.1, cov-2.6.1, assume-1.2.2
collected 16 items

*For this to show up, it takes 25 seconds, then tests run:*

Coverage disabled via --no-cov switch!

unknown file:0
unknown file:0: RemovedInPytest4Warning: config.warn has been deprecated, use warnings.warn instead

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================================== 12 failed, 4 passed, 2 warnings in 2.33 seconds ==============

/r/Python
https://redd.it/b78xba
Easy way for inline forms?

I'm creating an app and I am using some forms. Basic form are easy to add. But now I am trying some inline stuff. I want to be able to add everything in one form. For the admin page this is done automatically, and that form is exactly what I want:

​

class DomainAdmin(admin.ModelAdmin):
inlines = [
DiskInline,
NetworkInline,
]

​

My models:

​

class Domain(models.Model):
name = models.CharField(max_length=100)
cpu_cores = models.IntegerField()
cpu_threads = models.IntegerField()
cpu_type = models.CharField(max_length=255, default="", blank=True)
cpu_flags = models.CharField(max_length=255, default="", blank=True)
ram

/r/django
https://redd.it/b79yrr