【发布时间】:2017-10-21 17:54:31
【问题描述】:
我正在尝试扩展用户模型并添加一些字段,以下是我的方法:
Class UserProfile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
mobile_number = models.CharField(max_length=20)
gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
location = models.ForeignKey(Location, blank=True, null=True)
class User_One(UserProfile):
field_1 = models.CharField(max_length=20)
....
....
class User_Two(UserProfile):
field_1 = models.CharField(max_length=20)
....
....
所以基本上有两种类型的用户 User_One 和 User_Two 现在每当我们将这两种类型的用户保存到数据库中时,都会发生以下情况
-
User模型记录将使用值 1、2、3 等的单独 id 创建, -
User_One模型记录将使用 id 的 1,2,3 创建 -
User_Two模型记录将使用 id 的 1,2,3 创建
因此,对于每个模型记录的保存,Django 或数据库都会生成 id 的 1、2、3。
但是我有一个要求,用户模型应该为 id 字段值生成一个uuid 来代替整数,这可能吗?
我的意思是像下面这样
class User_Profile(models.Model):
id = models.IntegerField(default=uuid.uuid4)
【问题讨论】:
标签: python django django-models django-users