0

I've the following table:

# parts
- name (default)
- name_en (translated)
- name_es (translated)

When filling name_en and name_es via the django admin panel, name is also set, even if I keep it empty. The value is taken from either name_es or name_en, depending on my current locale.

As I've to support more than 2 languages and editors from several countries, the default field name would become a mess IMO.

Question

Is there a way to keep it empty or even avoid name_en and use name instead?

Code extraction

# settings.py
LANGUAGES = (
    ('en', gettext('English')),
    ('es', gettext('Spanish')),
)

# app/models.py
class Part(models.Model):
    name = models.CharField(max_length=100, blank=True, default='')

# app/serializer.py
class PartSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Part
        fields = ['name']

# app/views.py
class PartViewSet(viewsets.ModelViewSet):
    queryset = Part.objects.all()
    serializer_class = PartSerializer

Note: I'm using the django-restframework and the docs of django-modeltranslation mention:

When creating a new viewset , make sure to override get_queryset method, using queryset as a property won’t work because it is being evaluated once, before any language was set.

Thanks in advance!

4

0 回答 0