KristianOellegaard / django-hvad

Painless translations in django, using the regular ORM. Integrates easily into existing projects and apps. Easy convertible from django-multilingual-ng.
https://github.com/KristianOellegaard/django-hvad
Other
533 stars 128 forks source link

Django User Admin Translatable #272

Open ahmedsalman opened 8 years ago

ahmedsalman commented 8 years ago

I have extended django user model by adding it as foreign key in UserProfile Model. And I want to my User Admin to have all the fields of usermodels along with the fields of UserProfile.

Lucky which was working perfectly until i turn my UserProfile Model Translatable using django HVAD.

I am sharing my model along with admin code. Any suggestion to counter this issue is welcomed using django Hvad

class UserProfile(TranslatableModel):

    translations = CustomTranslatedFields(
        first_name=models.CharField(_('first name'), max_length=30, blank=True),
        last_name=models.CharField(_('last name'), max_length=30, blank=True),
        bio=models.TextField(
            verbose_name=_('Biography'),
            blank=True
        ),
        translation_created=models.DateTimeField(
            verbose_name=_('Creation date'),
            auto_now_add=True,
            editable=False,
        ),
        translation_created_by=models.ForeignKey(
            User,
            related_name="%(app_label)s_%(class)s_created_by",
            editable=False,
        ),
        translation_updated=models.DateTimeField(
            verbose_name=_('Update date'),
            auto_now=True,
            editable=False,
        ),
        translation_updated_by=models.ForeignKey(
            User,
            related_name="%(app_label)s_%(class)s_updated_by",
            null=True,
            blank=True,
        ),
    )
    user = models.OneToOneField(
        User,
        related_name='user_profile',
        verbose_name=_('User Profile'),
    )
    job_title = models.ForeignKey(
        Jobtitle,
        db_column='job_title',
        verbose_name=_('Job Title'),
    )
    company = models.ForeignKey(
        Company,
        db_column='company',
        verbose_name=_('company'),
        help_text=_("The company you were or are working at. If your entered company does not exist in the autocomplete dropdown, simply type it in and it will be added to the library automatically."),
    )

class CustomUserAdmin(TranslatableAdmin, UserAdmin):
    actions = None

    form = UserCustomChangeForm
    add_form = UserCustomCreationForm

    inlines = [
        UserProfileInline,
        UserPointsInline,
        UserExperienceInline,
        UserEducationInline,
        UserWebsitesInline,
        RelationshipInline,
    ]

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
spectras commented 8 years ago

Hello, Thanks for reporting here. Could you please describe the issue you have? The admin component is the one I am less familiar with in the codebase as it was written before I joined the project, so your problem is not obvious to me. Do you have errors? Unexpected behavior?

ahmedsalman commented 8 years ago

If i use the above mentioned code i am getting the error 'Exception Value: 'UserManager' object has no attribute 'untranslated'

but if i remove the parent class TranslatableAdmin from CustomUserAdmin system works fine.

spectras commented 8 years ago

Oh, alright. Well, TranslationAdmin only works on translatable models, so it won't work with User. That means you cannot inline a translatable model in a non-translatable admin form, unfortunately that's not supported. The issue is it would be inherently ambiguous which translations are to be used, since the main object does not have a language.

I would suggest one of the following approaches: