Accesorios de prueba de Django y tipos de contenido

I'm using django's Tipo de contenido foreign key in my model, and I'm using it in the fixtures for unit tests.

Therefore, I have to hard-code content_type_id in my fixture, but django sometimes initializes it to a different value and thus my tests fail.

So is there a way to safely predict the content_type_id of the model or any other proper way to handle such situations?

preguntado el 08 de noviembre de 11 a las 15:11

1 Respuestas

Use natural keys: https://docs.djangoproject.com/en/dev/topics/serialization/#natural-keys

Most of the documentation refers to how to add the capability to your own models, but ContentType already supports them so just add --natural-foreign para usted dumpdata management command:

$ python manage.py dumpdata myapp --indent=4 --natural-foreign

Respondido 11 Abr '16, 09:04

Thanks, a note that ContentType ya tienen get_by_natural_key(app_label, model) es muy útil. - dragón

Thanks, was stumped on this for a while. Also needed "--natural-primary" to get testing working. - hoju

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.