Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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模型引用同一类的属性中的另一个属性_Python_Django_Postgresql_Amazon Web Services_Amazon S3 - Fatal编程技术网

Python Django模型引用同一类的属性中的另一个属性

Python Django模型引用同一类的属性中的另一个属性,python,django,postgresql,amazon-web-services,amazon-s3,Python,Django,Postgresql,Amazon Web Services,Amazon S3,我试图为我的django web应用程序的用户配置文件详细信息构建一个模型,如: class UserDetails(models.Model): user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) profilePicture = models.ImageField(blank = True, upload_to='profile_pics/'+self.user.id+'/') co

我试图为我的django web应用程序的用户配置文件详细信息构建一个模型,如:

class UserDetails(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    profilePicture = models.ImageField(blank = True, upload_to='profile_pics/'+self.user.id+'/')
    country = models.CharField(max_length = 50, default='India')
    gender = models.CharField(max_length=10, default='NA')
    birthday = models.DateField(default=datetime.now())

    phone = models.CharField(max_length=15)
我在上面的模型中有一个图像字段,我想将传入的图像上传到媒体存储路径中的路径
profile\u pics/
。我试图通过将图像字段的
upload\u to
属性指定为
upload\u to='profile\u pics/'+self.user.id+'/'
来实现这一点。我正在使用AWS S3进行媒体存储,我已将必要的设置放入我的设置中,如下所示:

AWS_ACCESS_KEY_ID = 'myaccesskeyid'
AWS_SECRET_ACCESS_KEY = 'mysecretaccesskey'
AWS_STORAGE_BUCKET_NAME = 'mybucketname'

AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
尝试进行迁移时,出现以下错误:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/suraj/Work/treeapp/treeapp-backend/userMgmt/models.py", line 7, in <module>
    class UserDetails(models.Model):
  File "/home/suraj/Work/treeapp/treeapp-backend/userMgmt/models.py", line 9, in UserDetails
    profilePicture = models.ImageField(blank = True, upload_to='profile_pics/'+self.user.id+'/')
NameError: name 'self' is not defined
回溯(最近一次呼叫最后一次):
文件“manage.py”,第21行,在
main()
文件“manage.py”,第17行,主
从命令行(sys.argv)执行命令
文件“/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/site-packages/django/core/management/_-init__.py”,第401行,从命令行执行
utility.execute()
文件“/home/suraj/Work/treeapp/treeapp backend/treeEnv/lib/python3.6/site packages/django/core/management/_init__.py”,执行中第377行
django.setup()
文件“/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/site-packages/django/_u-init__;.py”,第24行,在安装程序中
应用程序。填充(设置。已安装的应用程序)
文件“/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/site-packages/django/apps/registry.py”,第114行,填充
app_config.import_models()
文件“/home/suraj/Work/treeapp/treeapp backend/treeEnv/lib/python3.6/site packages/django/apps/config.py”,第211行,导入模式
self.models\u module=导入模块(models\u module\u name)
文件“/home/suraj/Work/treeapp/treeapp-backend/treeEnv/lib/python3.6/importlib/__init__.py”,第126行,在导入模块中
return _bootstrap._gcd_import(名称[级别:],包,级别)
文件“”,第994行,在_gcd_导入中
文件“”,第971行,在_find_和_load中
文件“”,第955行,在“查找”和“加载”中解锁
文件“”,第665行,在“加载”中
exec_模块中第678行的文件“”
文件“”,第219行,在“调用”中,删除了“帧”
文件“/home/suraj/Work/treeapp/treeapp backend/userMgmt/models.py”,第7行,在
类用户详细信息(models.Model):
UserDetails中第9行的文件“/home/suraj/Work/treeapp/treeapp backend/userMgmt/models.py”
profilePicture=models.ImageField(空白=True,上传到@profile\u pics/'+self.user.id+'/'))
NameError:未定义名称“self”

请帮助我将上传到此模型的图像的默认路径设置为
profile\u pics/

您可以将可调用的

类用户详细信息(models.Model):
def配置文件图片上传(自我,文件名):
返回'profile_pics/{}/{}'。格式(self.user_id,文件名)
# …
profilePicture=models.ImageField(空白=真,上传到=profile\u图片上传)
注意:通常Django模型中字段的名称是用snake_大小写的,而不是PerlCase,因此它应该是:
profile_picture
,而不是
profilePicture

class UserDetails(models.Model):
    def profile_picture_upload(self, filename):
        return 'profile_pics/{}/{}'.format(self.user_id, filename)

    # …
    profilePicture = models.ImageField(blank=True, upload_to=profile_picture_upload)