I'm running into quite interesting situation.
I need to extend default django's Group model with some fields. I tried to use inheritance first, e.g. inherit from Group model and change some references, but seems I can't change all needed references, so, this way completely breaks django permission system.
Then I found this answer: How do I extend the Django Group model? where guy suggested to use field.contribute_to_class()
method.
I have put this adjustment right above the model definition in < myapp >. (don't ask me why do I need roles for group, it's not my idea, I just need them :D)
if not hasattr(Group, 'roles'):
field = models.ManyToManyField(
Role, verbose_name=_('roles'), blank=True,
help_text=_('List of roles attached to this group'),
related_name='groups')
field.contribute_to_class(Group, 'roles')
class MyGroup(Group):
class Meta:
proxy = True
def _sync_permissions(self):
"""
This method will sync group permissions with all attached Roles.
"""
self.permissions.clear()
for role in self.roles.all():
self.permissions.add(role.permissions)
self.save()
This part seems to be working (it really modifies django.contrib.auth.models.Group model)
But what I need next is to generate a migration for the Group model. If I simply run ./manage.py makemigrations <myapp>
it generates a migration for Group model, but tries to put it inside django.contrib.auth application, that is definitely not what I need.
So, my question here is:
Is there a way to tell django to generate a migration for Group model, but not to create a migration file under python libs directory, but rather create it inside < myapp > or just output the migration code?
the location where django looks for migrations cn be custominzed using MIGRATION_MODULES in your settings.py, anyway this means that ALL the migrations (not only the new) must be there. You need to copy the original migrations and manually update them when you upgrade Django
You can create a dedicated package so to not clash with your migrations Es.
MIGRATION_MODULES = {
'django.contrib.auth' : 'myapp.auth_migrations',
'myapp': 'myapp.migrations' # this line is only to clarify. IT'S NOT NEEDED AT ALL
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加