OneToOneField Django python3中没有'username'成员或fstring或自我属性问题

问题描述 投票:0回答:1

以下是我的代码。我为什么会出现错误

    from django.db import models
    from django.contrib.auth.models import User

    class Profile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        image = models.ImageField(default='default.jpg', upload_to='profile_pics')

        def __str__(self):
            return f'{self.user.username} Profile'

为什么我的F弦弄乱了?它说OneToOneField上的实例没有“用户名”成员?这是我如何调用配置文件-下面的代码-signals.py文件-

    from django.db.models.signals import post_save
    from django.contrib.auth.models import User
    from django.dispatch import receiver
    from .models import Profile

    @receiver(post_save, sender=User)
    def create_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)


    @receiver(post_save, sender=User)
    def save_profile(sender, instance, **kwargs):
        instance.profile.save()

因此在signals.py >>'Profile.objects.create(user = instance)'概要文件中不包含对象成员...

django python-3.7
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.