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
DjangoCon 2017 Videos are up and a question!

Firstly, I know a lot of people have been waiting, but the [recordings from DjangoCon 2017 are finally up!](https://www.youtube.com/channel/UC0yY6a79pPY9J0ShIHRf6yw/videos)

Secondly, I want to know from you, the community, what would you like to see covered next year?

/r/django
https://redd.it/6yio1q
How do I perform additional functions on my database ?

I have created a Django app that is based on a database on product sales. Besides, displaying the data in tables, I want to perform and show some statistics on them, ex. min, max, average etc.

I am not sure how to do that and where the code should reside, perhaps in views.py.

Below it is a simple example of what I have:

models.py

from django.db import models

class Sale(models.Model):
date = models.DateTimeField()
customer_name = models.CharField(max_length=264)
product = models.CharField(max_length=264)
amount = models.PositiveIntegerField()
value = models.DecimalField(max_digits=10, decimal_places=3)


views.py

from django.views.generic import DetailView, ListView
from . import models


class SalesView(ListView):
context_object_name = "sales"
model = models.Sale
template_name = "sales.html"

def get_queryset(self):
return models.Sale.objects.all().order_by("-timestamp")



/r/django
https://redd.it/6yjd9h
[AF] Flask.Session + ReactJS. Session not persisting after component change.

I have two components. Login and Main.

The initial view is Login, which sends a username and password to the Flask server using [Axios API ](https://github.com/mzabriskie/axios) and if the credentials are valid, a session is set, and the server sends back a JSON with a hashed user ID and status code.

The component then switches to Main. However when I make any HTTP request within the Main component, (POST, GET, DELETE), the session key which I made when login is valid no longer exists.

Could this problem be server-side?

/r/flask
https://redd.it/6yjul1
Python Slack Community: PySlackers. 8,500 users strong

We have commented a couple times about our python slack community, but I would like to make an official post about it now.

We are PySlackers, a community driven slack group of ~8,500 users from all over the world and all levels of experience. We have popular channels for most topics people care about such as:

#flask
#django
#devops
#async
#beer_geeks

and even many based on location like `#i18n_nyc` and more.


We would like to invite you all to our great community. You can find our website over at https://pyslackers.com where you can find a big button to automatically join our community. We also have a few community projects (`#community_projects`) where we are working on the website (django) and a slack bot (aiohttp) and are talking about others.

You can also find us on github at:
https://github.com/pyslackers

If you want to give back to the community and want to learn how to build things. We are also open to new ideas.

So on behalf of the other admins, we invite you all to join, and grow with us and the rest of the community and write some python!

Shawn (autoferrit)

/r/Python
https://redd.it/6yh5uy
Realtime Reddit Feed App

Hey everyone!

Just wanted to show off something useless as hell that I made, but kinda cool to look at. I use it to view the latest news coming in when I'm surfing the web. I'll glance back at it every few min to see what's happening in the news.

[Here's a video of it](https://streamable.com/5yst8). I'm in the process of getting hosting and then i'll have this running on a webpage showing news, and maybe have a separate page streaming /r/all, and i'll probably have it wipe that every couple hours.

I have a similar python script that can stream comments in real time from a particular subreddit or /r/all, and use keyword filters.

If i wanted to, I can easily make a bot out of these scripts and set up an auto-reply or do whatever to certain submissions or comment phrases.

I dont plan on making any money off of these. I'm hoping I can make some type of web app where people can pull up their own custom feed view and define what subreddits and/or keywords they want to filter out on their end.

/r/Python
https://redd.it/6yltvu
[AF] Jinja and Flask Routing

Hi, i'm currently trying to route something in my apps and i don't know how to make it work. I have this route in my flask app and it's working when i'm writing typing http://127.0.0.1:5000/user/Sam

@app.route('/user/<username>')
def crypto(username):

return render_template('crypto.html', username=username)


Where it's not working it's my Navbar i'm trying to make a link with Jinja to get to /user/Sam
<a class="nav-link" href="{{ url_for('user', username=username) }}">User</a>
But it's only give me http://127.0.0.1:5000/user/ what i'm missing to get the username to my link?

/r/flask
https://redd.it/6ylud9
Using email for authentication (with username now at 150 characters)

In my current app I don't need users to have a username (although there is the slight possibility that it may be needed later) and I would prefer them to signup with an email instead.

The docs and also this blog post show how to have authentication without a username, but it looks pretty convulted to get things working.
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#a-full-example
https://www.caktusgroup.com/blog/2013/08/07/migrating-custom-user-model-django/

However now that django has had the username limit lifted to 150 characters could I just set username field to email? and live with the outside risk of a long email

(- or do better approach of setting it to `sha256(user.email).hexdigest()[:150]`
and setting
```
class User(AbstractUser):
class Meta:
unique_together = ('email', )
```

from here: https://stackoverflow.com/questions/15597188/using-email-as-username-field-in-django-1-5-custom-user-model-results-in-fielder)

Is there any downside to this approach?


/r/django
https://redd.it/6ygh8m
Anyone using the Pyramid framework?

I believe that Reddit does (and do correct me if I'm wrong), but other than that it hardly gets any press like Django and Flask do? Is anyone using it? What is your experience like?

/r/Python
https://redd.it/6yn74i
Question: Let users sort model by input

Hey everyone, I'm having trouble finding this answer online so I thought I'd come here and ask. Thanks in advance.

I have a Django model based on real-world locations and addresses. I'd like the user to be able to input their zip-code, and have the model's objects be sorted by the closest available.

It seems like I could use a simple form to collect the user's zip code, but I'm not sure what to do next. Do I need to reload the html or is there a way to sort without changing the page?

Any advice would be appreciated. I don't feel like this should be too difficult, but I'm pretty new to the web-dev world.

/r/django
https://redd.it/6yp5ll
A way to auto generate more forms? Or is there another way?

Basically I want a user to be able to fill out an Invoice, and you can enter in Labor or a Product with a quantity and how much labor costs. There will be multiple products and labor codes.

Ie.

Quantity: 2 - Product Description - Unit Cost: $5/ea - Total: $10

Quantity: 10 - Product Description - Unit Cost: $.02/ea - Total: $0.20

Quantity: 1.5 - Labor Description - Per Hour: $35/hour - Total: $52.50


Should I have a row form generate after each one is filled out, or is there a better way to just enter all the data together in one form while being broken out for customer and business to see?

/r/django
https://redd.it/6ypzwq
DRF serialization not working on many to many with through models

I have models with many-to-many relationships with a through model. I am trying to set up a DRF serializer to display this data, but I am getting an error message whenever I try to render the API.

# models.py - simplified

class Person(models.Model):
first_name = models.CharField(max_length=250)
last_name = models.CharField(max_length=250)
status = models.IntegerField(choices=STATUS_CHOICES)
village = models.ForeignKey(Village)
gender = models.IntegerField(choices=GENDER_CHOICES)


class Case(models.Model):
summary = models.TextField()
session = models.ForeignKey(Session, on_delete=models.CASCADE)
case_type = models.ForeignKey(CaseType)
court_type = models.IntegerField(choices=COURT_TYPES)
verdict = models.ForeignKey(Verdict)
litigants = models.ManyToManyField(Person, through='Litigant', related_name='litigants')


class Litigant(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
case = models.ForeignKey(Case, on_delete=models.CASCADE)
role = models.ForeignKey(Role)
fine = models.ForeignKey(Money, null=True, related_name='fine')
notes = models.TextField()

My serializers.py looks as such:

# serializers.py - simplified

class LitigantSerializer(serializers.ModelSerializer):

class Meta:
model = Litigant
fields = ('id', 'person', 'case', 'role', 'fine', 'notes')


class CaseSerializer(serializers.ModelSerializer):

litigants = LitigantSerializer(many=True, read_only=True)

class Meta:
model = Case
fields = ('id', 'summary', 'session', 'case_type', 'court_type', 'verdict', 'litigants')


class PersonSerializer(serializers.ModelSerializer):

class Meta:
model = Person
fields = ('id','first_name', 'last_name', 'village', 'status', 'gender')

My views.py is:

# views.py - simplified.

class PersonViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all().order_by('village__name', 'last_name', 'first_name')
serializer_class = PersonSerializer


class CaseViewSet(viewsets.ModelViewSet):
queryset = Case.objects.all().order_by('session__village__name', 'session__date', 'court_type')
serializer_class = CaseSerializer


class LitigantViewSet(viewsets.ModelViewSet):
queryset = Litigant.objects.all().order_by('case__session__village__name', 'case__session__date', 'person__last_name',
'person__first_name')
serializer_class = LitigantSerializer

However, when I navigate to api/cases/ I receive the following error:

'RelatedManager' object has no attribute 'pk'

What is more annoying, is that when I only include id and notes in LitigantSerializer, only the id shows up... The notes are nowhere to be found.

Thank you for any help!


/r/django
https://redd.it/6yrh9k
PEP 554 -- Multiple Interpreters in the Stdlib
https://www.python.org/dev/peps/pep-0554/

/r/Python
https://redd.it/6yp7kv