无法为模型'userdetails'成功创建字段'user':未定义名称'UUID'

时间:2015-12-27 20:10:36

标签: python django django-models uuid

我被困在这里。找不到解决方案。我在models.py中有一个类:

进口

from django.contrib.auth.models import User
from django.db import models
from django.contrib.gis.db import models
from allauth.account.models import EmailAddress
from allauth.socialaccount.models import SocialAccount
from geoposition.fields import GeopositionField
from uuid import UUID
import uuid
import hashlib

型号:

class Pincode(models.Model):
    pincode = models.CharField("PinCode", max_length=6, null=False)
    geom = GeopositionField("Location")

    def __unicode__(self):
        return u'%s' % self.pincode


class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile')

    def __unicode__(self):
        return "{}'s profile".format(self.user.username)

    class Meta:
        db_table = 'user_profile'

    def profile_image_url(self):
        """
        Return the URL for the user's Facebook icon if the user is logged in via Facebook,
        otherwise return the user's Gravatar URL
        """
        fb_uid = SocialAccount.objects.filter(user_id=self.user.id, provider='facebook')

        if len(fb_uid):
            return "http://graph.facebook.com/{}/picture?width=40&height=40".format(fb_uid[0].uid)

        return "http://www.gravatar.com/avatar/{}?s=40".format(
            hashlib.md5(self.user.email).hexdigest())

    def account_verified(self):
        """
        If the user is logged in and has verified hisser email address, return True,
        otherwise return False
        """
        result = EmailAddress.objects.filter(email=self.user.email)
        if len(result):
            return result[0].verified
        return False


User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])


class UserDetails(models.Model):
    user = models.OneToOneField(User, related_name='User_Details',default= uuid.uuid4(),  unique=True)
    pin= models.ForeignKey(Pincode,related_name='pin', null= True, blank=True)
    rating= models.CharField(max_length=12, null=True, blank=True)


    def __unicode__(self):
        return u'%s, %s, %s' % (self.user,  self.pin, self.rating)


class Topmost(models.Model):
     pincode = models.ForeignKey(Pincode, unique=True)
     rating_pincode = models.CharField("Total Rating points", max_length=255, null=True, blank=True)
     frequency = models.CharField("Number of user", max_length=255, null=True, blank=True)
     normalized_rank = models.FloatField(null=True, blank=True)
     #objects = models.GeoManager()

     def __unicode__(self):
        return u'%s, %s, %s' % (self.pincode,  self.rating_pincode, self.frequency)

无法迁移它。显示错误:

ValueError: Cannot successfully create field 'user' for model 'userdetails': name 'UUID' is not defined.

On My Python IDLE:

>>> import uuid
>>> a = uuid.uuid4()
>>> type(a)
<class 'uuid.UUID'>

当我删除默认字段时,它也显示相同的错误。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

要创建OneToOne关系,您不应设置默认值。你必须这样使用它:

class UserDetails(models.Model):
    user = models.OneToOneField(User, related_name='User_Details',  unique=True)
    rating= models.CharField(max_length=12, null=True, blank=True)

    def __unicode__(self):
        return u'%s' % (self.user)

从标题中删除uuid导入(两行),然后重试。此模型中不需要uuid