farridav / django-jazzmin

Jazzy theme for Django
https://django-jazzmin.readthedocs.io
MIT License
1.62k stars 285 forks source link

How can i show the Profile image from entended User Model? #334

Open wrench1815 opened 3 years ago

wrench1815 commented 3 years ago

I've created a Model named Profile which has profile_image field on it. I use this extended model to set User Profile Images. Which can be accessed in User Model by profile.profile_image. But in Jazzmin settings if i set "user_avatar": "profile", It says there is no attribute url on User model. So after digging in Jazzmin project repo, i found this Templatetag which handles the fetching of User Avatar Image.

@register.simple_tag
def get_user_avatar(user: AbstractUser) -> str:
    """
    For the given user, try to get the avatar image
    """
    no_avatar = static("vendor/adminlte/img/user2-160x160.jpg")
    options = get_settings()

    if not options.get("user_avatar"):
        return no_avatar

    avatar_field = getattr(user, options["user_avatar"], None)
    if avatar_field:
        return avatar_field.url

    return no_avatar

Is it possible to override it so I can return avatar_field.profile_image.url instead? or is there a way to reference the Extended Model field? Or maybe I'm missing something? I did tried "user_avatar": "profile__profile_image" and "user_avatar": "profile_image" and "user_avatar": "profile.profile_image" just in case, but it didn't worked.

Thanks in Advance.

farridav commented 3 years ago

Hmm, it should have worked with "user_avatar": "profile_image"

Though this feature isn't used too often, and could do with better testing.

The code you referenced should pull the given attribute off of the implemented user model, and get its URL (assumes it's an ImageField)

It may be that it's not getting your custom user model some how, and is looking for the attribute on the default auth.user model ?

wrench1815 commented 3 years ago

It may be that it's not getting your custom user model some how, and is looking for the attribute on the default auth.user model ?

Yes that's what happening. It's Trying to look for that field on default auth.user while its on another another and its like auth.user.profile and the profile model has that imagefield for avatar.

farridav commented 3 years ago

I have worked on and merged in https://github.com/farridav/django-jazzmin/pull/338

are you able to install jazzmin from master pip install https://github.com/farridav/django-jazzmin/archive/master.zip and see if it fixes your problem ?

wrench1815 commented 3 years ago

My Apologies for the delay in reply> And yes it works now. Thanks for such a fast fix.

behshadrhp commented 1 year ago

Hi There :)

I ran into this problem and was guided to this page by chance.

To solve this problem, you only need to enter the field that contains the user's avatar photo.

Jazmin automatically recognizes the user's model and you only need to introduce that field to it .

for example:

My user model :

class User(AbstractUser):

    avatar = models.ImageField(upload_to='profile/')

    USERNAME_FIELD = 'username' 
    REQUIRED_FIELDS = ['email']

my jazzmin settings :

JAZZMIN_SETTINGS = {
    ....
    "user_avatar": "avatar",
   ....
}
ez4python commented 6 months ago
class User(AbstractUser):

    def avatar(self):
        return self.userphoto_set.last().image.url

class UserPhoto(Model):
    user = ForeignKey('apps.User', CASCADE)
    image = ResizedImageField(size=[736, 736], crop=['middle', 'center'], upload_to='users', blank=True, null=True)

If you have this type of code, you can easily get user image and show it in admin panel. In next picture you can see it result: Screenshot from 2024-04-27 20-26-55

6saifeddine9 commented 3 months ago

Capture d’écran 2024-07-06 185738 Capture d’écran 2024-07-06 185756 il change que comme ca il voulait pas s affihcher

6saifeddine9 commented 3 months ago

"user_avatar": "photo", class Profile(models.Model): account = models.OneToOneField(User, on_delete=models.CASCADE) phone=models.CharField(max_length=16) photo = models.ImageField(upload_to='user_photos/', null=True, blank=True)

def __str__(self) -> str:
    return self.account.username
6saifeddine9 commented 3 months ago

aider moi svp

6saifeddine9 commented 3 months ago

meme si je change ca "user_avatar": "fas fa-users-cog", il rest comme ca Capture d’écran 2024-07-06 185756