Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 709
Domen Blenkuš Avatar asked Nov 14 '25 21:11

Domen Blenkuš


1 Answers

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
like image 154
Alasdair Avatar answered Nov 17 '25 11:11

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!