gpt4 book ai didi

python - ValueError : Could not find function url in draco. apps.home.models

转载 作者:太空宇宙 更新时间:2023-11-04 01:13:23 27 4
gpt4 key购买 nike

我只是扩展我的用户模型,添加用户、照片、电话、电子邮件等字段。当我使用“./manage.py makemigrations”命令在控制台中进行迁移时,我的问题就出现了。完整的消息是:

ValueError: Could not find function url in dracoin.apps.home.models.
Please note that due to Python 2 limitations, you cannot serialize unbound method functions (e.g. a method declared
and used in the same class body). Please move the function into the main module body to use migrations.

这是我的“models.py”(我相信这个 .py 是错误的根源):

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


class userProfile(models.Model):

def url(self,filename):
ruta = "MultimediaData/Users/%s/%s"%(self.user.username,filename)
return ruta

user = models.OneToOneField(User)
photo = models.ImageField(upload_to=url)
phone = models.CharField(max_length=30)
email = models.EmailField(max_length=75)

def __unicode__(self):
return self.user.username

我是 django 和 python 的新手,如果我忽略了什么,请提前致歉。

谢谢!!

最佳答案

错误消息实际上告诉您问题出在哪里 - photo 字段定义中的 url 是绑定(bind)方法,无法序列化- 它甚至为您提供了解决方案,即将方法从类中移到主函数中。这意味着:

def url(obj, filename):
ruta = "MultimediaData/Users/%s/%s"%(obj.user.username,filename)
return ruta

class userProfile(models.Model):

user = models.OneToOneField(User)
photo = models.ImageField(upload_to=url)

关于python - ValueError : Could not find function url in draco. apps.home.models,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26263356/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com