#23422: Cannot add Permission to Group in data migration
----------------------------+--------------------------------------
     Reporter:  tjwalch     |                    Owner:  nobody
         Type:  Bug         |                   Status:  new
    Component:  Migrations  |                  Version:  1.7
     Severity:  Normal      |               Resolution:
     Keywords:              |             Triage Stage:  Unreviewed
    Has patch:  0           |      Needs documentation:  0
  Needs tests:  0           |  Patch needs improvement:  0
Easy pickings:  0           |                    UI/UX:  0
----------------------------+--------------------------------------
Changes (by chriscauley):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Comment:

 I'm a bit new to the migrations system, but I was able to verify this. I
 created a new app called "my_app" and a model called "MyModel". I created
 the first migration and then the following data migration:


 {{{
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals

 from django.db import models, migrations

 def make_permissions(apps,schema_editor):
     Group = apps.get_model("auth","Group")
     Permission = apps.get_model("auth","Permission")
     my_new_group = Group(name="test_group")
     my_new_group.save()
     my_new_group.permissions.add(
 Permission.objects.get(codename='add_mymodel',content_type__app_label='my_app')
     )

 class Migration(migrations.Migration):

     dependencies = [
         ('my_app', '0001_initial'),
         ('auth', '0001_initial'),
     ]

     operations = [
         migrations.RunPython(make_permissions,reverse_code=lambda
 *args,**kwargs: True)
     ]
 }}}

 If I run these separately (`python manage.py migrate my_app 0001;python
 manage.py migrate my_app 0002`), everything is cool. Together it breaks
 (`python manage.py migrate my_app`). You could solve this yourself by
 doing a get_or_create on the content type and on the permission, but that
 is by no means an adequate solution.

--
Ticket URL: <https://code.djangoproject.com/ticket/23422#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.be9bfefe2e0f5d486bc69d2ebdce3081%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to