Batman
Batman

Reputation: 8927

Should Meta Classes in Models Inherit From Object

It seems pretty conventional to write models something like this:

from django.db import models

class Foo(models.Model):
     name = models.CharField(max_length=100, null=False)

     class Meta:
         ordering = ("name")

Is there any reason class Meta: is used, and not class Meta(object):? Is there any reason not to explicitly inherit from object?

Upvotes: 4

Views: 199

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599610

As well as Stephen's answer, note that from version 2.0, Django only supports Python 3; in that version of Python there is no need to inherit from object, all classes are automatically "new-style".

Upvotes: 1

Stephen Rauch
Stephen Rauch

Reputation: 49794

Inheriting from object should make no difference. The pattern:

class Foo():
    class Meta:
       attribute = 'This is Interesting'

Is primarily to make is easy to later code something like:

if Foo.Meta.attribute == 'How Boring':
    ....

In this pattern the only functionality is accessing the class attributes of Meta so any other functionality of Meta probably doesn't matter.

Upvotes: 1

Related Questions