我有三种模型,当用户注册此部件时,我想创建它们来创建Artist和UserProfile,而我也想在更新UserProfile时更新Artist模型。
models.py:
自定义用户模型:
class CustomUser(AbstractBaseUser, PermissionsMixin):
artist_choice = [
(0, 'celebrities'),
(1, 'singer'),
(2, 'comedian'),
(3, 'dancer'),
(4, 'model'),
(5, 'Photographer')
]
artist_category = models.IntegerField(choices=artist_choice, null=True)
email = models.EmailField(max_length=100, unique=True)
name = models.CharField(max_length=100)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
last_login=models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
EMAIL_FIELD='email'
REQUIRED_FIELDS=[]
objects=UserManager()
def get_absolute_url(self):
return "/users/%i/" % (self.pk)
艺术家模型:
class Artist(models.Model):
CHOICES = (
(0, 'celebrities'),
(1, 'singer'),
(2, 'comedian'),
(3, 'dancer'),
(4, 'model'),
(5, 'Photographer')
)
name = models.CharField(max_length=100)
artist_category = models.IntegerField(choices = CHOICES, null=True)
artist_image = models.ImageField(upload_to= 'media',null=True)
bio = models.TextField(max_length = 500)
def __str__(self):
return self.name
用户个人资料模型:
class UserProfile(models.Model):
CHOICES = (
(0, 'celebrities'),
(1, 'singer'),
(2, 'comedian'),
(3, 'dancer'),
(4, 'model'),
(5, 'Photographer')
)
user = models.OneToOneField(CustomUser, related_name='userprofile', on_delete= models.CASCADE)
image = models.FileField(upload_to=None, max_length=100)
artist_category = models.IntegerField(choices= CHOICES, null=True)
mobile_number = PhoneNumberField(null=True)
country = CountryField(default = 'IN')
city = models.CharField(blank=True, max_length=100)
bio = models.TextField(blank=True)
def __str__(self):
return self.user.name
用户注册后创建个人资料:
def create_profile(sender,**kwargs ):
if kwargs['created']:
user_profile=UserProfile.objects.get_or_create(user = kwargs['instance'])
post_save.connect(create_profile,sender=CustomUser)
这是我遇到错误的部分:
@receiver(post_save,sender=CustomUser)
def create_user_artist(sender, instance,created, **kwargs):
if created:
data = {
'name': instance.name,
}
Artist.objects.create(**data)
@receiver(post_save,sender=UserProfile)
def update_user_artist(sender, **kwargs):
instance = kwargs['instance']
instance.artist.artist_category = instance.artist_category
instance.artist.save()
我正在尝试在注册用户时获取名称,并在更新个人资料时显示artist_category,但显示错误
答案 0 :(得分:0)
错误本身很明显,您正在尝试使用artist_category
对象访问UserProfile
。
需要与UserProfile
建立Artist
的外交关系。
class UserProfile(models.Model):
CHOICES = (
(0, 'celebrities'),
(1, 'singer'),
(2, 'comedian'),
(3, 'dancer'),
(4, 'model'),
(5, 'Photographer')
)
user = models.OneToOneField(CustomUser, related_name='userprofile', on_delete= models.CASCADE)
image = models.FileField(upload_to=None, max_length=100)
artist = models.ForeignKey(Artist,related_name='user_artist')
artist_category = models.IntegerField(choices= CHOICES, null=True)
mobile_number = PhoneNumberField(null=True)
country = CountryField(default = 'IN')
city = models.CharField(blank=True, max_length=100)
bio = models.TextField(blank=True)
def __str__(self):
return self.user.name
制作-
@receiver(post_save,sender=CustomUser)
def update_user_artist(sender, **kwargs):
instance = kwargs['instance']
instance.artist.artist_category = instance.artist_category
instance.artist.save()