Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 下载的Django项目文件引发RelatedObjectDoesNotExist_Python_Django_Django Models - Fatal编程技术网

Python 下载的Django项目文件引发RelatedObjectDoesNotExist

Python 下载的Django项目文件引发RelatedObjectDoesNotExist,python,django,django-models,Python,Django,Django Models,我的电脑上正确运行了此项目。但是,当其他人拉取/下载此项目文件时,createsuperuser之后会显示RelatedObjectsDoesNotExist Models.py class User(AbstractBaseUser): email = models.EmailField( verbose_name='email address', max_length=255, unique=True, ) first_

我的电脑上正确运行了此项目。但是,当其他人拉取/下载此项目文件时,createsuperuser之后会显示RelatedObjectsDoesNotExist

Models.py

class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    first_name =models.CharField(max_length=100)
    middle_name =models.CharField(max_length=100)
    last_name =models.CharField(max_length=100)

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    is_chairperson = models.BooleanField(default=False)
    is_faculty = models.BooleanField(default=False)
    is_student = models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'middle_name', 'last_name']

class FacultyInfo (models.Model):
    fac_user = models.OneToOneField(User, on_delete=CASCADE, primary_key=True)

@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
    if created:
        if instance.is_faculty == True:
            FacultyInfo.objects.create(fac_user=instance)
        instance.facultyinfo.save()

完全回溯

Traceback (most recent call last):
  File "E:\iPLM-master2\iPLM-master\manage.py", line 22, in <module>
    main()
  File "E:\iPLM-master2\iPLM-master\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in exe
cute
    return super().execute(*args, **options)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 189, in ha
ndle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "E:\iPLM-master2\iPLM-master\CRS\models.py", line 33, in create_superuser
    user = self.create_user(
  File "E:\iPLM-master2\iPLM-master\CRS\models.py", line 26, in create_user
    user.save(using=self._db)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
    super().save(*args, **kwargs)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 726, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 774, in save_base
    post_save.send(
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\dispatch\dispatcher.py", line 180, in send
    return [
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\dispatch\dispatcher.py", line 181, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "E:\iPLM-master2\iPLM-master\CRS\models.py", line 149, in create_or_update_user_profile
    instance.facultyinfo.save()
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\related_descriptors.py", line 421, in _get_
    raise self.RelatedObjectDoesNotExist(
CRS.models.RelatedObjectDoesNotExist: User has no facultyinfo.


根据它们,错误仍然存在。我确实看到错误发生在接收器上。但是,我不明白为什么它对我有效,而对使用同一文件的其他人无效。

只有在
instance.is
为真但调用了
instance.FacultyInfo.save()
时,才创建
FacultyInfo
实例,看到问题了吗?事实上,您甚至需要在那里调用
instance.facultyinfo.save()
,因为调用
create
会保存到数据库中……哦,天哪,缩进是错误的。同时感谢您清理
创建
。我希望我能对你的评论投赞成票,但非常感谢!
def has_user(self):
        return  self.user_id is not None