Невозможно добавить/определить select_related with в кодовой базе django
Это мой views.py, названный здесь admin.py
class BaseMembershipInline(ReadOnlyMixin, admin.TabularInline):
class MembershipInlineFormSet(BaseInlineFormSet):
def get_queryset(self):
print("(((((((((((GET Q SET)))))))))))))))")
# Orders by property field
super().get_queryset()
self._queryset = sorted(
super().get_queryset(), key=lambda membership: membership.order
)
return self._queryset
model = Membership
formset = MembershipInlineFormSet
print("FORMSET ", formset)
extra = 0
@classmethod
def get_current_membership_ids(cls):
# TODO Does this SELECT all accounts?
print("))))))))GET OWNER((((((((((((")
return Account.objects.all().values_list("current_membership_id")
def change(self, obj):
print("_____CHANGE ____________")
# add custom redirect url to redirect admin to membership tab of account
# detail page
change_membership_url = reverse(
"admin:accounts_membership_change", args=[obj.id]
)
redirect_url = (
reverse("admin:accounts_account_change", args=[obj.account_id]) + "#tabs-2"
)
link_url = change_membership_url + f"?{REDIRECT_FIELD_NAME}={redirect_url}"
return format_html('<a href="{}">{}</a>', link_url, "Change")
class Membership(AbstractBaseModel):
# Billing frequency choices
BILLING_FREQUENCY_MONTHLY = "monthly"
BILLING_FREQUENCY_YEARLY = "yearly"
BILLING_FREQUENCY_ONCE = "once"
BILLING_FREQUENCY_NEVER = "never"
BILLING_FREQUENCY_CHOICES = [
(BILLING_FREQUENCY_MONTHLY, "Monthly"),
(BILLING_FREQUENCY_ONCE, "Once"),
(BILLING_FREQUENCY_NEVER, "Never"),
(BILLING_FREQUENCY_YEARLY, "Yearly"),
]
# All database fields
account = models.ForeignKey(
"Account", related_name="memberships", on_delete=models.CASCADE
)
plan = models.ForeignKey(
"MembershipPlan", related_name="memberships", on_delete=models.PROTECT
)
next_membership = models.OneToOneField(
"self",
related_name="previous_membership",
null=True,
blank=True,
on_delete=models.DO_NOTHING,
)
Это наследуемый класс для каждой модели в моем models.py
class AbstractBaseModel(LogEntryMixin, models.Model):
id = models.BigIntegerField(
primary_key=True, default=generate_unique_id, editable=False
)
creation_datetime = models.DateTimeField(auto_now_add=True, editable=False)
update_datetime = models.DateTimeField(auto_now=True)
objects = BaseModelManager()
class Meta:
abstract = True
class BaseModelQuerySet(QuerySet):
def get_or_none(self, **kwargs):
try:
return self.get(**kwargs)
except self.model.DoesNotExist:
return None
class BaseModelManager(Manager):
def get_queryset(self):
return BaseModelQuerySet(self.model, using=self._db)
def get_or_none(self, **kwargs):
return self.get_queryset().get_or_none(**kwargs)
/cp/accounts/account/1234567899008998/change/
1005ms overall +158ms
55ms on queries +38ms
51 queries +64
Я потратил много времени, пытаясь отладить или реверсировать свой код при запросе к вышеуказанному url, т.е. /cp/accounts/account/id/change/, однако не смог найти ни одной точки в коде, где я могу добавить select_related или prefetch_related, чтобы решить эту проблему N + 1 запроса. Любая помощь будет принята с благодарностью.