Search code examples
djangodjango-fixturesdjango-guardian

How to reference ContentType in Django's fixture?


I'm writing tests for Django project with django-guardian. Fixture for permissions look like:

- fields:
    content_type: 10
    object_pk: '2'
    permission: 22
    user: 1
  model: guardian.userobjectpermission
  pk: 1

Is it safe to reference content_type and permission by id? Or are ids going to change (i.e. when I add extra permission/model)?

What is the best method for referencing objects which are loaded automatically?


Solution

  • You should be able to use natural keys to reference the content_type and permission values.

    - fields:
        content_type: [<app_label>, <model>],
        object_pk: '2'
        permission: [<permission_codename>, <app_label>, <model>],
        user: 1
      model: guardian.userobjectpermission
      pk: 1