Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django testing and ContentType generic relatations fixtures

How do I use GenericRelations in Django unit testing?

I have read and tried countless suggestions on the internets to no avail.

This one was promising Problems with contenttypes when loading a fixture in Django

but "reset" command is no longer there in Django 1.6

My project uses GenericForeign key to ContentType and a Foreign key to auth.Users. I dumped the data only for my app using the --natural option but this lead to the problem where users didn't exist in the test database. Now I removed the --natural option and instead dumped data for all 3 apps myApp, auth, contenttypes. When I'm running the test I get "Could not load contenttypes.ContentType(pk=50): columns app_label, model are not unique". I think this is due to the contenttypes being created dynamically when models are imported.

The command used:

python manage.py dumpdata auth myApp contenttypes --natural --indent=4 > auhtmtmnatural.json

Next I dumped myApp, auth leaving off contenttypes. I figured that since ContentTypes app db is created dynamically and my fixtures have natural keys, this should work. It doesn't.

Now I'm getting:

DeserializationError: Problem installing fixture 'auhtmtmnatural.json': ContentType matching query does not exist.

How do I get fixutres with contetypes to work? Is it better to use setup and create objects in testing?

like image 667
user3138929 Avatar asked Mar 21 '14 15:03

user3138929


1 Answers

Use content_type: [<app_label>, <model>], instead of content_type: <hard-coded pk of model>

like image 184
rohanagarwal Avatar answered Oct 04 '22 15:10

rohanagarwal