Ignore base model in Django ORM request

I have these models:

class Foo(models.Model):
    some_field = models.CharField()
    class Meta:
        pass

class Bar(Foo):
    some_other_field = models.CharField()
    class Meta:
        pass

The example is simplified, in fact, both models have many fields.

When I request Bar, Django ORM creates a request containing an internal connection to Foo.
I do not need information in Foo.

Question : Is there a way to request Bar without internally connecting to Foo?

I understand that removing a bar that extends Foo and makes it a foreign key would be the best way to solve this problem. However, there is a lot of legacy code based on this, so I would prefer a quick solution until I have the time and courage to reorganize the outdated parts of the application.

I also understand that I can write the SQL query myself, but I would prefer a solution using ORM.

+4
3

, , -

class SkinnyBar(models.Model):
    some_other_field = models.CharField()

    class Meta:
        managed = False
        db_table = "app_bar"

ORM.

,

class BaseBar(models.Model):
    some_other_field = models.CharField()

    def some_common_method(self):
        return True

    class Meta:
        abstract = True


class Bar(BaseBar, Foo):

    def some_method_that_requires_foo(self):
        return self.some_field == 1


class SkinnyBar(BaseBar):

    class Meta:
        managed = False
        db_table = "app_bar"
+3

- Foo, abstract = True . some_field app_foo, Bar . some_field, SQL. , app_bar app_foo.

0

, .

, . , , ? , ?

I'm not sure if this will work, but you can always use the defer ' function in your request. This feature is valid only for extended use cases and may not apply here. Essentially, deferral does not attempt to request the fields you specify. if you are defer'some_field ', it may not make a connection. The disadvantage is that as soon as you try to access "some_field" from the object, it will execute the request (iteration will cause n additional requests).

0
source

All Articles