search

Home  >  Q&A  >  body text

Save user profile model containing image and user fields as OneToOneField variable in Django Rest Framework

The user model is as follows.

class User(AbstractUser):
    username = None
    email = models.EmailField('email address', unique=True)
    first_name = models.CharField('First Name', max_length=255, blank=True,
                                  null=False)
    last_name = models.CharField('Last Name', max_length=255, blank=True,
                                 null=False)
    objects = UserManager()

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

The user profile model is as follows.

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
    avatar = models.ImageField(upload_to=avatar_image, blank=True, null=True)

P粉111227898P粉111227898234 days ago398

reply all(1)I'll reply

  • P粉268654873

    P粉2686548732024-03-31 09:32:13

    This makes sense. Please note that django uses the PIL library to handle images

    In your virtual environment pip install pillow In your models.py

    from PIL import images

    Images are handled as static files. Handling static files is described here

    reply
    0
  • Cancelreply