models中的pk主键用法

class FrontUserModel(models.Model):
    uid = models.UUIDField(primary_key=True,default=uuid.uuid4)
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=20)
    password = models.CharField(max_length=128)
    avatar = models.URLField(blank = True)
    is_active  = models.BooleanField(default=True)
    date_joined = models.DateTimeField(auto_now_add=True)

user = FrontUserModel.objects.filter(pk=request.pk).first()
在练习过程中采用username__pk.会报错:FieldError: Unsupported lookup 'pk' for CharField or join on the field not permitted.
#user = FrontUserModel.objects.filter(username__pk=request.pk).first()
原因是因为:username是一个CharField字段,字段是没有pk属性的。只有外键才会有pk属性
posted @ 2017-04-24 11:55  村里汉子  阅读(406)  评论(0编辑  收藏  举报