Question

I'm writing some tests and I need to create some objects but I get this error when I try to create some object outside a Django View

cat = Category.objects.create(catalog=c, name="Category one")

Returns

TypeError: 'name_es' is an invalid keyword argument for this function

This fails with modeltranslation 0.6.1 but works with 0.3.2

name is a field translated with modeltranslation.

From the docs:

The unittests use the django.utils.translation.trans_real functions to activate and deactive a specific language outside a view function.

I've tried this:

trans_real.activate('es')
cat = Category.objects.create(catalog=c, name="Category one")

And I get the same error :(

Anyone knows a better way for testing modeltranslation based models in Django?

EDITED

More things tried so far:

cat = Category.objects.create(**{'catalog':c, 'name': 'Category one'})
TypeError: 'name_es' is an invalid keyword argument for this function
Was it helpful?

Solution

You can check out the solution by looking at the docs here

If you want to create a category name in all the languages:

x = Category.objects.populate(True).create(name='Category one')

And if you want to create it in a specific language:

x = Category.objects.create(name_en='Category one')

This works for django-modeltranslation version 0.6+

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top