deschler / django-modeltranslation

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

Allow either default field in admin #80

Open deschler opened 12 years ago

deschler commented 12 years ago

Original author: chriseva...@gmail.com (July 05, 2012 02:13:54)

Model with a required field. A number of fields are then created by modeltranslation and in admin it requires that the default language field is used. Would it be possible to make it so it is accepted if at least one of the fields are entered?

i.e.

in models.py name = models.CharField(max_length=255)

In the admin pages this will show up as:

Name-en required

Name-es

Preferably an option should allow for either field to fulfil the requirement.

Original issue: http://code.google.com/p/django-modeltranslation/issues/detail?id=80

deschler commented 12 years ago

From eschler on July 06, 2012 21:38:11 Basically that would require to add a form with a clean method.

In addition, and that is the easier part, all translation fields have to be made optional. This can already be achieved by overriding formfield_for_dbfield and setting field.required to False, like this:

def formfield_for_dbfield(self, db_field, **kwargs):
    field = super(TranslationBaseModelAdmin, self).formfield_for_dbfield(
        db_field, **kwargs)
    self.patch_translation_field(db_field, field, **kwargs)
    if hasattr(field, 'required'):
        field.required = False
    return field

As i'm currently working on some bigger admin refactoring i will see if this feature can be added in a sensible way.

eliasfernandez commented 11 years ago

Hi deschler, i override only translated fields with that condition:

if  hasattr(db_field, "translated_field")  and  hasattr(field, 'required'):

and works fine

def formfield_for_dbfield(self, db_field, **kwargs):
    field = super(OneRequiredTranslationMixin, self).formfield_for_dbfield(db_field, **kwargs)
    self.patch_translation_field(db_field, field, **kwargs)
    if  hasattr(db_field, "translated_field")  and  hasattr(field, 'required'):
        field.required = False
    return field