deschler / django-modeltranslation

Translates Django models using a registration approach.
BSD 3-Clause "New" or "Revised" License
1.38k stars 257 forks source link

How to keep the default field empty? #601

Closed JimmyTheNerd closed 3 years ago

JimmyTheNerd commented 3 years ago

Hi folks!

First of all, thank you for creating this package!

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. It's also redundant.

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, but the caveat mentioned in your docs shouldn't affect the current issue, afaik:

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!

last-partizan commented 3 years ago

I think there is no way to keep it empty or use name as name_en.

But this should not be a problem, as name will get replaced by translation when needed.