3

I've posted this on django-users but haven't received a reply!

So I have my own profile objects for users (subclass of User). One of the fields is imagefield, which is (obviously) used for users to upload their logo/thumbnail.

The question is how I can include this on their comments?

Any ideas? Thanks in advance!

Markos Gogoulos
  • 371
  • 1
  • 4
  • 12

1 Answers1

10

Subclassing User is not recommended in Django. Instead, create a separate profile model.

Let's assume you have your app, foo.

In foo's models.py, add:

class FooUserProfile(models.Model):
   user = models.ForeignKey(User, unique=True)
   user_image = models.ImageField()

Then, in your settings.py, add:

AUTH_PROFILE_MODULE = 'foo.FooUserProfile'

Now, whenever you have a user object, you can get its profile object with the get_profile() function. In your case, in the template you could add:

<img src="{{ comment.user.get_profile.user_image }}"/>

A caveat: you will need to create a FooUserProfile and associate it with your User any time you create new User.

You can read more in the Django documentation, Storing additional information about users or in the article Django tips: extending the User model

Matthew Christensen
  • 1,741
  • 12
  • 15