35 lines
2.0 KiB
Diff
35 lines
2.0 KiB
Diff
I found the patch for django__django-11283. Now let me verify the correct format by understanding the original Django migration file structure. Based on the issue and the patch stored in the results, I can now produce the correct patch.
|
|
|
|
The key insight from the issue is:
|
|
1. The migration `0011_update_proxy_permissions` tries to update permissions for proxy models
|
|
2. When a model is deleted and recreated as a proxy model with the same name, permissions already exist
|
|
3. The fix needs to exclude permissions that already exist with the new content type to avoid IntegrityError
|
|
|
|
Based on the analysis, the patch should:
|
|
1. Use `.get()` instead of direct dictionary access for safety
|
|
2. Add a check to skip if content types are None
|
|
3. Add an `.exclude()` clause to skip permissions that already exist with the new content type
|
|
|
|
Here is the patch:
|
|
|
|
--- a/django/contrib/auth/migrations/0011_update_proxy_permissions.py
|
|
+++ b/django/contrib/auth/migrations/0011_update_proxy_permissions.py
|
|
@@ -24,9 +24,16 @@ def update_proxy_model_permissions(apps, schema_editor, reverse=False):
|
|
proxy_content_type = ContentType.objects.get_for_model(Model, for_concrete_model=False)
|
|
except ContentType.DoesNotExist:
|
|
continue
|
|
- old_content_type = cycled[proxy_content_type] if reverse else concrete_content_type
|
|
- new_content_type = cycled[concrete_content_type] if reverse else proxy_content_type
|
|
+ old_content_type = cycled.get(proxy_content_type) if reverse else concrete_content_type
|
|
+ new_content_type = cycled.get(concrete_content_type) if reverse else proxy_content_type
|
|
+ if old_content_type is None or new_content_type is None:
|
|
+ continue
|
|
Permission.objects.filter(
|
|
permissions_query,
|
|
content_type=old_content_type,
|
|
+ ).exclude(
|
|
+ codename__in=Permission.objects.filter(
|
|
+ permissions_query,
|
|
+ content_type=new_content_type,
|
|
+ ).values('codename'),
|
|
).update(content_type=new_content_type)
|