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
Model objects to be grouped by date field in another model

Hello everyone,

I had two models, one called reviews and the other called answers, as you can see from its names, one review has many answers ( exactly 7) a classic one to many relationship.

I'm able to do all the common stuff with this straight forward relationship, filtering, prefech_related, even prefetching only some answers with a condition.

Now, I want to do a group by review date with the answers objects in that day, as you can see, my problem is how I can do that because I will group many objects with just one field, the normal aggregate in Django are just sum and count, but I want the objects

I was searching for a while about this and I can't see a solution where this can be done with orm to produce a queryset then serialize it or in SQL

Thank you

**Edit:**

To summarize my problem in simple words, I want to get all the objects of the group by field, for ex: if the date 2019-12-12 had 10 answers then rather than the normal annotations which do count or sum I want to get these 10 objects

Thank you again

Edit 2:

Here is my review model


``

class Review(model.Model):

#other fields omitted



/r/django
https://redd.it/e9v6cm
GitHub - MTrajK/coding-problems: Python solutions for various coding/algorithmic problems and many useful resources for learning algorithms and data structures

Hi, I decided to make public my GitHub [repo](https://github.com/MTrajK/coding-problems) which I was using to store my solutions for various coding/algorithmic problems and I updated the README file with many useful resources for learning algorithms and data structures.

Currently, there are 115 solutions (but I'm planning to add more solutions in the future).

All solutions:

* are written in [Python](https://github.com/MTrajK/coding-problems#solutions) (because it's one of the simplest and the most readable languages).
* are following the same [template](https://github.com/MTrajK/coding-problems#template).
* are [categorized](https://github.com/MTrajK/coding-problems#categories) (belonging in one of the 7 defined categories).
* are solved in the best way (the optimal time and space complexity), some of them are solved in more than one way (like [nth\_fibonacci\_number.py](https://github.com/MTrajK/coding-problems/blob/master/Other/nth_fibonacci_number.py), there are shown 8 different solutions for this problem, starting from the worst O(2\^N) solution and ending with the best O(LogN) solution).

As I said previously, there are many [resources](https://github.com/MTrajK/coding-problems#learning-resources) for learning algorithms and data structures. They are divided into 4 categories:

* [Courses](https://github.com/MTrajK/coding-problems#courses) (collection of free courses from one of the best CS universities)
* [Books](https://github.com/MTrajK/coding-problems#books) (several books that have made an impression on me)
* [Training Sites](https://github.com/MTrajK/coding-problems#training-sites) (more than 40 platforms with coding/algorithmic problems, like LeetCode)
* [Other Resources](https://github.com/MTrajK/coding-problems#other-resources)

I hope that this project will help someone who is preparing for coding interviews or someone who wants to

/r/Python
https://redd.it/ebf7rc
My User model has 3 IntegerFields - n_correct_questions , n_incorrect_questions, and n_total_questions. How can I display a sorted leaderboard of Users according to the percentage of questions they answered correctly?

I tried to define the percentage of questions correctly answered in the User model itself by doing

class User(AbstractUser):
#other fields
n_correct_questions = models.IntegerField()
n_incorrect_questions = models.IntegerField()
n_total_questions = models.IntegerField()

correct_percentage = self.n_correct_questions*100/self.n_incorrect_questions

but Django gives me an NameError saying 'self' is not defined.

File "P:\Web\truetest\truetest\home\models.py", line 170, in User
correct_percentage = self.n_correct_questions*100/self.n_questions_answered
NameError: name 'self' is not defined

I have a leaderboard view where I want to show a leaderboard of users sorted by the percentage of questions they have answered correctly. How can I sort users according to their percentage? Moreover, why does Django throw that NameError?

/r/django
https://redd.it/j919ms
Smartcut: Super fast cutting and trimming of videos

What My Project Does

Smartcut is to my knowledge the most robust open-source implementation of frame accurate video cutting without recoding, a.k.a smart cut, smart encoding, smart render, etc.

It uses PyAV and libavcodec (ffmpeg internals) to encode a small part of the video near the cutpoints and then uses libavformat to stitch the recoded segments and parts of the original video back together into a whole video.

https://github.com/skeskinen/smartcut

Target Audience

This project is for people who want to cut videos really fast from the command line. This could be useful e.g. as part of a script that goes through a directory and quickly cuts off some part of the videos. It could also be used as a part of a video editor project, like I've done in my GUI video editor project.

It is also one of the largest available projects that uses PyAV (the pythonic bindings for libav project) and really showcases the awesomeness of the library. I also contributed 4 patches to PyAV and the maintainer was really cool to work with.

Comparison

The github page has a pretty nice list of related projects: https://github.com/skeskinen/smartcut?tab=readme-ov-file#other-projects

The most obvious comparison is to lossless-cut which is a popular open-source video editor written in TypeScript and Electron

/r/Python
https://redd.it/1f767df
Django app deletion from a django project

I need to remove a Django app from my project. This app has ForeignKey relationships with models in other apps. Several migrations involving this app have already been applied to the database. I'm concerned about data loss in the related models of other apps if I simply delete the app.
- What's the safest and most recommended way to remove an app, considering the existing ForeignKey relationships and migrations?
- What are the best practices, and what should I avoid doing to prevent data corruption or loss?
- Is it possible to keep the old data of deleted models?

---


I have tried these steps but face some dependency issues which need manual intervention. I want to know the recommended ways.

1. Delete dependent migrations for dependent_app
`rm -rf dependent_app/migrations`

2. Make necessary changes for the models of dependent_app
3. Recreate migrations
`python manage.py makemigrations dependent_app`

4. Delete all migrations for the my_app
`rm -rf my_app/migrations`


5. Apply fresh migrations
`python manage.py migrate --fake`


6. Remove imports, urls and other associations

7. Remove from INSTALLED_APPS

```
INSTALLED_APPS = [
#Other apps
'my_app', # Remove this line
]
```


/r/djangolearning
https://redd.it/1iw2ned