Being able to automatically create few groups with predefined permissions (including permissions created automatically by django.contrib.auth.signals).
Apparently there are two ways to do this:
The problems with going with the first option are:
add_, delete_, change_). - "pk" field in the fixture, giving that some permissions have already been created.The problem with going with the second option is:
django.contrib.auth.signals, therefore you are not able to call its predefined permissions and add them to your groups.Not only that, but with a simple:
def create_initial_groups(**kwargs):
staff_group = Group(name = 'Staff')
clients_group = Group(name = 'Clients')
staff_group.save()
clients_group.save()
and:
signals.post_syncdb.connect(
create_initial_groups,
dispatch_uid = 'unique_app_identifier.listeners.create_initial_groups'
)
I get an:
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_group_name_key" DETAIL: Key (name)=(Staff) already exists.
Even with a clean database (running drop schema public cascade; create schema public; on psql before python manage.py syncdb).
So, what's the cleanest way, if any exists, to prepopulate the database with few groups and permissions?
If you can associate those custom permissions with your models, try defining them under the permissions attribute inside model's Meta class.
Create fixtures for your custom groups, but use natural keys when serializing them, so that they refer to related permissions by their name rather than pk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With