2

我只是扩展了我的用户模型,添加了用户、照片、电话、电子邮件等字段。当我使用“./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 的新手,如果我忽略了什么,请提前道歉。

谢谢!!

4

1 回答 1

3

错误消息实际上确实告诉您问题所在 -urlphoto字段的定义中是一个绑定方法,无法序列化 - 它甚至为您提供了解决方案,即将方法移出类进入主函数。这意味着:

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)
于 2014-10-08T18:13:23.893 回答