Skip to main content
10 votes
Accepted

Building Slack message

I've come back to this question about 4 or 5 times since it was originally posted, started writing up an answer, started doubting my alternative implementation, scrapped it, and then come back about a ...
Dan Oberlam's user avatar
  • 7,814
9 votes
Accepted

Raise error when slug already exists

The good So, you're trying to allow users to create new questions, but those questions must have a slug which is unique. You're enforcing this on the database level with ...
Kevin Brown-Silva's user avatar
8 votes
Accepted

Unpacking tuple from Django's get_or_create function

I'd say this is a good example on how to not use DB. The problem here is that for each keyword you will call a DB instead of perform this as a single call, so if you list of keywords is 1000 items ...
Alex's user avatar
  • 1,622
7 votes

Implementation of lru_cache for daily "top categories" query

From what I understand, the lru_cache is not the right tool when it comes to caching things in a production web application. Depending on how the web application is ...
alecxe's user avatar
  • 17.3k
6 votes
Accepted

Password generator in Django

Don't reinvent the wheel Since you're handling a security feature, you shouldn't roll your own. As often with python, Django comes bateries included. And since they already need to generate random ...
301_Moved_Permanently's user avatar
6 votes
Accepted

Civil status database with Django

Few things here: 1. Parent1 and Parent2 It's not really clear why would you need to have parents in the separate tables while they both represent a person. There is no difference between parent1 and ...
Alex's user avatar
  • 1,622
6 votes

Building Slack message

We can learn a lot about what code is supposed to do by giving our functions explicit type signatures. Starting at the top, what are we working with? team_id is ...
ShapeOfMatter's user avatar
6 votes
Accepted

Summing categories of financial records per month in a query

Mixing Portuguese and English makes this code messy. Please pick one or the other. I recommend coding consistently in English, because Python, its library, and its code ecosystem are in English, so ...
200_success's user avatar
5 votes
Accepted

Generating a unique ID for a filename

Code review: Well, your code seems ok, it can be improved: You can replace the letters by string.ascii_letters You can remove ...
Laurent LAPORTE's user avatar
5 votes

Method to return the lowest filename number not being used

When you don't need indexes or ordering, it is generally a good idea to use a set over a list because its membership testing is more efficient. That means, use ...
zondo's user avatar
  • 3,797
5 votes
Accepted

Is this "smart" way of populating the admin page ok code-wise?

The usual "django" way of doing this - is to use get_app_config() function to get the AppConfig instance, then call ...
alecxe's user avatar
  • 17.3k
5 votes
Accepted

Showing different forms for user type with Django/Python in views.py

Dicts are good for lookups When encountering stacked ifs that are all alike, I tend to pickup a dict and try to put it to good use. One way to structure the ...
Stephen Rauch's user avatar
5 votes
Accepted

Preventing code duplication in Django

Try this: ...
Solomon Ucko's user avatar
  • 1,566
5 votes
Accepted

Cleanest way to get list of Django objects that have same foreign key and display them in a view

However, I am not sure if passing the interviews QuerySet like that is a good practice or not. This is good practice, known as dependency injection. If someone asks for your business card, you wouldn'...
James Hiew's user avatar
5 votes
Accepted

Let's make this Order nicer

There's a lot of duplication here for like items. A sign that loops and templates will reduce redundancy. There's a few distinct steps here. Better to separate each step to separate concerns. For ...
six8's user avatar
  • 236
5 votes
Accepted

Get list of claims based on priorities

The function seems to be inconsistent; I don't know what is it intended to do, so if you say it works as expected, probably you can lose some functionality on unnecessary "beautification" of ...
Pavlo Slavynskyy's user avatar
4 votes
Accepted

Validating Email Addresses without DB

As Sjoerd points out in comments, the timestamp generation and checking logic in this code is reimplementing Django's built-in TimestampSigner. Standard ...
Gareth Rees's user avatar
  • 49.6k
4 votes
Accepted

Django query filter on count in one to many relation

It's easier to answer this kind of question if you give us more code. I would have preferred the question to contain (possibly cut-down) code for your models, not just a summary description of the ...
Gareth Rees's user avatar
  • 49.6k
4 votes
Accepted

Storing database references to files/media

This part here is strange: obj.image.media.all()[0].images.all() If by design, Post.image only links to media holding images, ...
Ext3h's user avatar
  • 2,799
4 votes
Accepted

Django Forms and handling

Naming C’est vraiment bizarre to have some of the mots in French et d’autres in English. I don't talk about the templates, where it is normal to use the language of the end user, but rather your <...
301_Moved_Permanently's user avatar
4 votes

Test Driven Development with Django

The code has a lot of repetitive blocks, a test flow is mixed up with the data and the logic making it difficult to follow, the number of different assertions makes the tests bigger and more ...
alecxe's user avatar
  • 17.3k
4 votes
Accepted

Update model Django

You can use .update() queryset method and unpack the request.data dictionary: ...
alecxe's user avatar
  • 17.3k
4 votes
Accepted

Implementation of lru_cache for daily "top categories" query

Answering your questions: You state your goal as wanting to show: top categories by products' view / sell counts but what you actually show is: ten categories chosen at random from the ...
Gareth Rees's user avatar
  • 49.6k
4 votes
Accepted

CreateView set foreign key from URL parameter

There's no need to add the ipsum instance to the template context unless it's used in the template. Adding things to self is the ...
Matt's user avatar
  • 247
4 votes

Django Rest Framework - ManyToMany relationship through intermediate model

Django-organizations is a great way to do this, if you didn't want to reinvent the wheel. Comes with many things I assume you would need in the future out of the box.
Kevin Galloway's user avatar
4 votes
Accepted

Tournament Management model

Here are few thoughts how the code could be slightly refactored. The calculation of YEAR_CHOICES is little bit ugly. You could do it more elegant with a list ...
cezar's user avatar
  • 262
4 votes
Accepted

NHL Stats web app

At a high level you definitely have the right idea about pulling things out of your views. In MVC, we generally keep business logic out of views. That includes things like calculating player stats and ...
Bailey Parker's user avatar
4 votes

How can I refactor the validation code to minimize it?

Exception handling You may want to consider reducing the try block to where the expected exception actually is raised. ...
Richard Neumann's user avatar
4 votes
Accepted

Email verification resend implementation Django

You have a problem in the post() method. If the user does not exist you return self.form_valid(form), so if a new user in ...
reza mahmoudian's user avatar
3 votes

Password generator in Django

The code in the post is insecure because it uses the built-in function random.choice to select the characters in the password. If you had read the documentation for ...
Gareth Rees's user avatar
  • 49.6k

Only top scored, non community-wiki answers of a minimum length are eligible