# get\_latest\_by doesn't work properly when inherited from parent class

**URL:** <https://forum.djangoproject.com/t/get-latest-by-doesnt-work-properly-when-inherited-from-parent-class/5651>\
**Category:** Using Django\
**Created:** [December 11, 2020, 5:25am UTC](https://forum.djangoproject.com/t/get-latest-by-doesnt-work-properly-when-inherited-from-parent-class/5651 "2020-12-11T05:25:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wacko\_artist](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/wacko_artist/32/2785_2.png) [@wacko\_artist](https://forum.djangoproject.com/u/wacko_artist)\
**Post date:** [December 11, 2020, 5:25am UTC](https://forum.djangoproject.com/t/get-latest-by-doesnt-work-properly-when-inherited-from-parent-class/5651/1 "2020-12-11T05:25:18Z")

</div>

Hi there, my problem is simple but I’m not able to figure it out.

So I have a Base model defined like

```
class BaseModel(models.Model):
    time = models.DateTimeField(auto_now_add=True)
    tags = JSONField(blank=True, null=True)
    location = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        abstract = True
        get_latest_by = "time"
        ordering = ["-time"]

```

I was hoping to import this into multiple apps and so that I don’t have to rewrite the code.  
The Child class is something like this

```
Class MyChildModel(BaseModel):
    language = models.CharFeild(max_length=255)
    ... more fields....
    
    Class Meta:
         db_table = "child_data_table"

```

The problem is when I’m trying to do

`MyChildModel.objects.latest()`

I encounter the error

`ValueError: earliest() and latest() require either fields as positional arguments or 'get_latest_by' in the model's Meta`

I believe get\_latest\_by should have been inherited. I even check in manage.py shell, it says the Meta is present based upon time. Is there something that I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 11, 2020, 5:29am UTC](https://forum.djangoproject.com/t/get-latest-by-doesnt-work-properly-when-inherited-from-parent-class/5651/2 "2020-12-11T05:29:19Z")

</div>

Please see the docs on [Meta inheritance](https://docs.djangoproject.com/en/3.1/topics/db/models/#meta-inheritance).

---

<div class="post-metadata">

**Author:** ![wacko\_artist](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/wacko_artist/32/2785_2.png) [@wacko\_artist](https://forum.djangoproject.com/u/wacko_artist)\
**Post date:** [December 11, 2020, 8:24am UTC](https://forum.djangoproject.com/t/get-latest-by-doesnt-work-properly-when-inherited-from-parent-class/5651/3 "2020-12-11T08:24:13Z")

</div>

Hi Ken,

I have made the changes in the exact same way, but it gives the latest and earliest error. On checking the MyChildModel.Meta.get\_latest\_by I’m getting time. Which is correct but why the views are not reading it correctly is the problem.

I hope you will understand my predicament.

Thank you

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 11, 2020, 1:31pm UTC](https://forum.djangoproject.com/t/get-latest-by-doesnt-work-properly-when-inherited-from-parent-class/5651/4 "2020-12-11T13:31:20Z")

</div>

Can you post your current models? (And to avoid the typos that were in your previous post, please copy/paste the code and not retype it. Also remember to enclose the code between lines of three backticks - ` to maintain formatting.)

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [December 11, 2020, 3:58pm UTC](https://forum.djangoproject.com/t/get-latest-by-doesnt-work-properly-when-inherited-from-parent-class/5651/5 "2020-12-11T15:58:58Z")

</div>

The `Meta` class for `MyChildModel` needs to inherit from `BaseModel.Meta`. It’s a small gotcha that’s easy to overlook in the documentation.

```auto
class MyChildModel(BaseModel):
    ...
    class Meta(BaseModel.Meta):
         ...

```

---

<div class="post-metadata">

**Author:** ![wacko\_artist](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/wacko_artist/32/2785_2.png) [@wacko\_artist](https://forum.djangoproject.com/u/wacko_artist)\
**Post date:** [December 11, 2020, 4:58pm UTC](https://forum.djangoproject.com/t/get-latest-by-doesnt-work-properly-when-inherited-from-parent-class/5651/6 "2020-12-11T16:58:29Z")

</div>

Hey @CodenameTim, I knew I was missing something. You’re a life saver. Thank you for you insight, much appreciated 🙂
