Heim  >  Fragen und Antworten  >  Hauptteil

Speichern Sie das Benutzerprofilmodell mit Bild- und Benutzerfeldern als OneToOneField-Variable im Django Rest Framework

Das Benutzermodell ist wie folgt.

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']

Das Benutzerprofilmodell ist wie folgt.

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粉111227898186 Tage vor332

Antworte allen(1)Ich werde antworten

  • P粉268654873

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

    这是有道理的。请注意,django 使用 PIL 库来处理图像

    在你的虚拟环境中 pip 安装pillow 在你的 models.py

    from PIL import images

    图像作为静态文件处理。 此处描述了处理静态文件

    Antwort
    0
  • StornierenAntwort