문제

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
도움이 되었습니까?

해결책

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+

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top