To have a way to modify third part model classes
|Reported by:||Marinho Brandão||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Sometimes we need to modify a model class from a third part application or a Django's contrib, just for a specific project. The most common situations are related to the User, Group and Permission classes, like add new fields, or change unicode, etc.
So I wrote this metaclass to allow us to modify a model class from another place of code without really change the original code.
Basically you just inherit ModifiedModel, set a Meta inner class with model = 'application.Model' and set attributes with model fields, like below:
from django.db import models
from django.contrib.auth.models import User
from modify_models import ModifiedModel
model = User
exclude = ('first_name','last_name',)
website = models.URLField(blank=True, verify_exists=False)
return '%s - %s'%(self.pk, self.name)
The code above adds a new field "website", excludes "first_name" and "last_name" and replaces the descriptor unicode in the User model.
In my projects and some of my clients sometimes we need this, so I thought it can be useful for other people and could be part of Django.
I'm attaching a simple Python file to this ticket.
If/when the ticket be approved I can work on documentations and tests and maybe improve the code to be part of the framework (i.e. re-think names).
Change History (7)
comment:1 Changed 6 years ago by
|Patch needs improvement:||unset|