New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using assign_perm in django 1.7 migration #281
Comments
+1 |
2 similar comments
+1 |
+1 |
I fixed this error by importing |
I've ended up using two helper functions for migration scripts: |
Still no love with 1.8/1.3 unfortunately. Using your helper functions I get the error
|
Unfortunately, using assign_perms inside migrations (or any other schema dependent code not in the migration itself) is a really bad idea. I am not surprised it doesn't work either. The reason it doesn't work it most likely in the logic to find the permission model isn't using the correct model returned by However, there is a fundamental problem here that is not solvable. That is the database migrations do not attempt to serialize the In fact, it is possible some the the requested features, such as roles, could require this change in the schema. This is probably not the answer you were looking for, however I think you have to use lower level database operations to set the permissions. By this I mean directly interaction with the Permission db models. Django migrations should not be accessing database schema dependent functions outside the migration. This includes custom functions on the db model class also. Possibly this limitation could be better documented. Pull requests appreciated. As such closing this bug. |
hello |
Maybe auth groups could be wrapped to behave like a guardian group. |
I am posting this here in case it's useful for anyone else, but I wrote this relatively generic def assign_perm(perm, user_or_group, obj=None, apps=None):
permission_model = apps.get_model('auth', 'Permission')
permission = permission_model.objects.get(codename=perm)
# Assumes object permission models live in the same app as the model they govern
# and are named like `ProjectUserObjectPermission`/`ProjectGroupObjectPermission`
obj_permission_model = apps.get_model(obj._meta.app_label, '{}{}ObjectPermission'.format(
obj._meta.object_name,
user_or_group._meta.object_name,
))
obj_permission_model.objects.create(
# `object_pk` and `content_type_id` keys if you're not using direct foreign keys
# https://django-guardian.readthedocs.io/en/stable/userguide/performance.html#direct-foreign-keys
content_object=obj,
permission=permission,
**{
user_or_group._meta.model_name: user_or_group
}
) |
Consider the following migration code:
When applying this migration,
assign_perm
call gives an error:The text was updated successfully, but these errors were encountered: