Save user profile model containing image and user fields as OneToOneField variable in Django Rest Framework
P粉111227898
P粉111227898 2024-03-30 23:56:05
0
1
431

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粉111227898
P粉111227898

reply all(1)
P粉268654873

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template