ValueError:在draco.apps.home.models中找不到函数url

时间:2014-10-08 17:56:13

标签: python django migration django-models django-migrations

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

谢谢!

1 个答案:

答案 0 :(得分:3)

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

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)