Skip to content

Instantly share code, notes, and snippets.

Anybody have experience with a Max Recursion error when using defer?
https://dpaste.org/1o8W -- This code will work as-is, I've ran into a Max Recursion error when __init__ has TWO fields it's trying to access
https://dpaste.org/bdXc -- here is the code that errors, I've removed the comment in Person.__init__ and now there are TWO fields it's trying to access vs ONE
I guess I'm more asking to help understand why ONE vs TWO fields causes the recursion error. I see refresh_from_db getting called and from the docs I've read it makes sense because it does that for the field that it doesn't have (first_name), then in the __init__ it looks like it is calling "only" with the field "has_siblings"
Then looks like it's trying to call refresh_from_db on the second field in the __init__ "suffix" with deferred fields having "has_siblings"
If anybody could confirm that analysis and if this is intended behavior and just poor performance OR if this appears to be an actual bug and I should open a ticket for it?
Here is a prof
@michaelhelmick
michaelhelmick / models.py
Last active April 2, 2021 20:59
game/views.py
from django.utils.translation import gettext_lazy as _
class Game(models.Model):
TEEBALL_INTRODUCED_VERSION = "1.1.0"
REVAMPED_PRODUCT_INTRODUCED_VERSION = "2.0.0"
class GameType(models.TextChoices):
BASEBALL = "BB", _("Baseball")
SOFTBALL = "SB", _("Softball")
@michaelhelmick
michaelhelmick / views.py
Created April 2, 2021 20:51
Bad example of views.py
from rest_framework import viewsets
class GameViewSet(viewsets.ReadOnlyModelViewSet):
"""Game viewset."""
queryset = Game.objects.all()
serializer_class = GameSerializer
def get_serializer_class(self):
"""Determine serializer class."""
@michaelhelmick
michaelhelmick / views.py
Last active April 2, 2021 20:44
Bad example
from rest_framework import viewsets
class GameViewSet(viewsets.ReadOnlyModelViewSet):
"""Game viewset."""
queryset = Game.objects.all()
serializer_class = GameSerializer
def get_queryset(self):
"""Filter class queryset."""
@michaelhelmick
michaelhelmick / views.py
Last active April 2, 2021 20:44
views.py
from rest_framework import viewsets
from semantic_version import SimpleSpec, Version
class GameViewSet(viewsets.ReadOnlyModelViewSet):
"""Game viewset."""
queryset = Game.objects.all()
serializer_class = GameSerializer
def get_queryset(self):
@michaelhelmick
michaelhelmick / models.py
Last active April 2, 2021 20:32
game/models.py with TeeBall
from django.utils.translation import gettext_lazy as _
class Game(models.Model):
TEEBALL_INTRODUCED_VERSION = "1.1.0"
class GameType(models.TextChoices):
BASEBALL = "BB", _("Baseball")
SOFTBALL = "SB", _("Softball")
TEEBALL = "TB", _("Tee-ball")
@michaelhelmick
michaelhelmick / models.py
Created April 2, 2021 13:38
game/models.py
from django.utils.translation import gettext_lazy as _
class Game(models.Model):
class GameType(models.TextChoices):
BASEBALL = "BB", _("Baseball")
SOFTBALL = "SB", _("Softball")
type = models.CharField(
max_length=2,
@michaelhelmick
michaelhelmick / versioning_example_v2.py
Created April 2, 2021 13:21
versioning_example_v2.py
def get_serializer_class(self):
if self.request.version in ['v1', 'v2']:
return AccountSerializerVersion1
return AccountSerializer
@michaelhelmick
michaelhelmick / versioning_example.py
Created April 2, 2021 01:20
versioning_example.py
def get_serializer_class(self):
if self.request.version == 'v1':
return AccountSerializerVersion1
return AccountSerializer
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.pythonPath": "<path-to-env>"
}
}