I assume you are using the build-in django User
model.
Django User model doesn't have an avatar field. So what you should do is extend your default User model by creating a One-to-one relationship to another model with the avatar field
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
avatar = models.ImageField(upload_to = "avatars/", null=True, blank=True)
then use the django post_save
signal to automatically create the UserProfile
modal instance when a User
instance is created.
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from yourapp.models import UserProfile
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
after that you'll be able to access the avatar field with the Modal Forms...
~hope that helps
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…