Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django User Admin Translatable #272

Open
ahmedsalman opened this issue Dec 23, 2015 · 3 comments
Open

Django User Admin Translatable #272

ahmedsalman opened this issue Dec 23, 2015 · 3 comments

Comments

@ahmedsalman
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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:

  • Use your UserProfile model instead as main editing point. This might be troublesome though as Django's admin does not support nested inlines.
  • Implement a custom admin subclass that knows about loading the correct translations based on your project's rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants