get_or_create created 2 instances [Sloppy Development]

I am a woman enough to talk about this. I usually don’t let these things happen, but this is such a small thing that let me down on a really high amount of requests.

The statistics of an API I was using showed that at 00:00 UTC we stopped using it altogether.

So, the culprit is the simplest model ever:

class Date(models.Model):
    date = DateField()

And I only ever created it (or altered) like this:

    date, cr = Date.objects.get_or_create(
        day=f'{this_moment.year}-{this_moment.month}-{this_moment.day}'

As it always happens, 4am Sunday it well went to hell – Django create two instances of ‘2021-08-15’.

Eventually every requests that had this line in it just crashed.

Why did this happen? Well, apparently two instances are not supposed to be made.

My friends and I came to a conclusion that with a very high amount of requests Django missed that tiny bit of time between executing get_or_create method and actually writing an instance to PostgreSQL.

Well good thing - I went to bed late, and my team woke me up much earlier than I would wake up.

What is he solution? If the field is meant to be unique, make it unique:

class Date(models.Model):
    date = DateField(unique=True)

[Note] ‘ChatConsumer’ has no attribute ‘as_asgi’ – Sloppy development

Following Channels’ docs I encountered yet another error the solution to what was not obvious right off the bat.

Ditch Channels.

The End.

This was my original post in October 2020 and I stand by it.

You can make your project work with Google Firebase - it handles signals well enough. Yes it is primarily used by mobile developers, but overall it is fine for web too.

I spent trying to install Django Channels on my local Linux 18.04, and I kind of made it work, but then I had to deploy it... And as a professional backender you don't want to let people down.

I decided to try Channels because my Python-related friends talked about it. But days into the trouble I asked my friend about a certain step and he asked me in return "oh so [you use it because] they fixed Channels since two years ago?". I as like ??????????????????. So that moment I ditched it. Too much time wasted, but I like the result I achieved with Firebase.

What I am saying - Firebase is fast and universal. In a matter of days I managed to make and deploy an internal Customer Support chat service that connected mobile app users and a support from the web.</p>

I will be writing a new series on Django + Firebase, and this post is pretty much a short introduction.

Django + Froala: style fix

Froala provides a great WYSIWYG editor for Django admin, but on page the styles for django-froala-editor didn’t work no matter what I tried. It didn’t look like what I get at all.

At some point, it was clear that providing a quick fix would suffice. I made a short style sheet to make these styles work on Django templates. It is my example that fits my needs. I don’t eliminate the fact that I failed to find an organic solution to this via package files.

Read More »

GenericView: Attributeerror ‘list’ object has no attribute ‘filter’ // 1-second guide 😮💢

So you are casually overriding get_queryset() in a, say, ListView, like this:

def get_queryset(self):
    entries = [x for x in Entry.objects.all() if x.publications]
    return entries

Because you need to sort the Entry table by a reverse M2M relation publications and then BAM!:

Attribute error 'list' object has no attribute 'filter'

Without a trace of the line you made a mistake in…

Read More »

Pycharm insists on running one of the project scripts with every Django command // Sloppy Development 🤭

I am starting the Careless Development series where I post a little issue that started as a problem solving and ended up being an oversight issue. Utter disappointment! It took me some time to inspect Pycharm inside and out, so here it is.

I should say that the bigger the project, the more it happens. But enough of self-justification.

Read More »