Define Django Base Model Class

49 views
Skip to first unread message

Guilherme Leal

unread,
Dec 28, 2016, 12:14:00 PM12/28/16
to Django users
In one of my django projects, I have a base model (that inherits from django.db.models.Model), and is the base model class that all my models uses.
I would like to "override" the base model class from django, so everytime a developer would import the base model class with "from django.db.models import Model", it would insted import my project's custom base model.

The result would be something like this:

from django.db import models

class MyModel(models.Model):
   
pass


>>> type(MyModel)
<class 'MyProject.MyApp.CustomModel'>


Any help would be apreciated!

Avraham Serour

unread,
Dec 28, 2016, 12:47:42 PM12/28/16
to django-users
You could monkeypatch django to accomplish this, but I would advise against it.

Instead of having implicit or hidden behaviour you should instruct your developers to use from myproject import models instead of from django.db import models


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/649b3b97-17d3-4257-b473-f050440b4d14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Guilherme Leal

unread,
Dec 28, 2016, 1:03:27 PM12/28/16
to django...@googlegroups.com
Got it.

The custom model doesn't have any significantly different behaviour, just some helper properties that return, for instance, a basic GrapQL Node from that very model (the project uses graphene).

Vijay Khemlani

unread,
Dec 28, 2016, 4:13:50 PM12/28/16
to django...@googlegroups.com
That doesn't change the advise

I would find it weird to import a class only to have something else loaded


"Explicit is better than implicit."


Reply all
Reply to author
Forward
0 new messages