【问题标题】:AttributeError: 'NoneType' object has no attribute 'save' djangoAttributeError:“NoneType”对象没有属性“保存”django
【发布时间】:2019-12-10 08:45:56
【问题描述】:

我有三个模型。当我创建一个User 时,它将创建一个UserProfile。当UserProfile 被创建时,它应该创建一个Artist。一切正常,但当我更新 UserProfile 时出现错误。

models.py:

class Artist(models.Model):
      CHOICES = (
             (0, 'celebrities'),
             (1, 'singer'),
             (2, 'comedian'),
             (3, 'dancer'),
             (4, 'model'),
             (5, 'Photographer')
               )


      name = models.CharField(max_length=100,null= True)
      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 str(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)
      artist = models.ForeignKey(Artist,related_name='userprofile', on_delete=models.CASCADE, null=True)

      name = models.CharField(max_length=100, null=True)
      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 str(self.user)

def create_profile(sender,instance, created,**kwargs):
    if created:
       data = {
            'name': instance.name
              }

    UserProfile.objects.create(user=instance,**data)
post_save.connect(create_profile,sender=CustomUser)

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


def create_artist(sender,instance,created,**kwargs):
    if created:
       data = {
           'name': instance.name,
           'artist_category': instance.artist_category,
           'bio': instance.bio
           }
       Artist.objects.create(**data)        

   post_save.connect(create_artist,sender=UserProfile)

@receiver(post_save, sender= UserProfile)
def save_artist(sender,instance,**kwargs):
    instance.artist.save()

它给了我这个错误:

`File "/home/tboss/Desktop/environment/celeb/celeb/main_site/models.py", line 155, in create_artist
instance.artist.save()
AttributeError: 'NoneType' object has no attribute 'save'

`.................................................. …………

【问题讨论】:

  • 请向我们展示完整的追溯。
  • 查看编辑..
  • 这意味着UserProfile 实例没有artist...
  • 但它已经添加到用户配置文件模型中

标签: django python-3.x


【解决方案1】:

问题在于,在您的第一个post_saveUserProfile 信号中,您正在创建一个新的Artist 实例,但没有连接你的UserProfile 实例。

删除create_artist 和save_artist 信号并添加以下内容:

@receiver(post_save, sender= UserProfile)
def update_artist(sender,instance,created,**kwargs):
    if created:
       data = {
           'name': instance.name,
           'artist_category': instance.artist_category,
           'bio': instance.bio
           }
       instance.artist = Artist.objects.create(**data)
       instance.save()
    else:
        instance.artist.save()

同一模型有两个 post_save 信号,可以合并为一个。

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 2019-10-18
    • 2015-07-31
    • 2018-02-11
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 2016-09-11
    相关资源
    最近更新 更多