db.models.options help with a piece of code analysis.

100 views
Skip to first unread message

Elton Pereira de Lima

unread,
Jan 15, 2016, 10:36:45 AM1/15/16
to Django developers (Contributions to Django itself)
What's up guys!

I am studying the core of django and I came across a piece of code that confused me.


If meta_attr is a copy of self.meta .__ dict__, then when will the marked section will run?

Thanks!

charettes

unread,
Jan 15, 2016, 12:13:50 PM1/15/16
to Django developers (Contributions to Django itself)
Hi Elton,

From a quick look this branch seems to handle attributes inherited from
possible `Meta` bases.

e.g.

class Foo(models.Model):
    class Meta:
        app_label = 'app'

class Bar(models.Model):
    class Meta(Foo.Meta):
        pass

assert 'app_label' not in Bar.Meta.__dict__
assert Bar.Meta.app_label == 'app'

Cheers,
Simon

Elton Pereira de Lima

unread,
Jan 18, 2016, 7:35:30 AM1/18/16
to Django developers (Contributions to Django itself)
Thanks for help!

Elton Pereira de Lima

unread,
Jan 18, 2016, 12:34:29 PM1/18/16
to Django developers (Contributions to Django itself)
Hello charettes!

Analyzing the code further, I saw that it was impossible for the Bar Meta class inherits from Foo.Meta because when this code is executed, the Meta class ceases to exist because of this line.

Marten Kenbeek

unread,
Jan 18, 2016, 12:47:22 PM1/18/16
to Django developers (Contributions to Django itself)
Hi Elton,

Django makes the Meta class of abstract models available as Foo.Meta. This allows you to define common Meta options on the abstract base class, and inherit the base Meta in your concrete child models. So the above example won't work as you noted, but this will:

class Foo(models.Model):
   
class Meta:
        app_label
= 'app'

       
abstract = True


class Bar(models.Model):
   
class Meta(Foo.Meta):
       
pass

assert 'app_label' not in Bar.Meta.__dict__
assert Bar.Meta.app_label == 'app'

charettes

unread,
Jan 18, 2016, 1:09:16 PM1/18/16
to Django developers (Contributions to Django itself)
Hi Elton,

From what I understand the line you pointed at doesn't delete the model `Meta`
attribute but the `_meta.meta` attribute which makes it possible for `Bar.Meta`
to subclass `Foo.Meta` even if `Foo.Meta.abstract is not True`.

Simon

charettes

unread,
Jan 18, 2016, 1:14:06 PM1/18/16
to Django developers (Contributions to Django itself)
FWIW, as Marteen pointed out, the `Meta` attribute is only available on
abstract classes because of these lines.

Simon

Elton Pereira de Lima

unread,
Jan 18, 2016, 1:23:44 PM1/18/16
to Django developers (Contributions to Django itself)
Really, it's true!

Thanks for help!
Reply all
Reply to author
Forward
0 new messages