So I am able to generate a random id using uuid So far so good But when I try to database i get same value
所以我能够使用uuid生成一个随机id到目前为止很好但是当我尝试数据库时,我得到相同的值
def f():
d = uuid4()
str = d.hex
return str[0:16]
class Q(models.Model):
a = models.CharField(max_length=150)
b = models.IntegerField(max_length=25)
c = models.IntegerField(max_length=32 , default=0)
d = models.ManyToManyField(Ans , related_name='aa')
e = models.CharField(max_length=18 , default = f() ,unique=True )
class Ans(models.Model):
sub = models.CharField(max_length=150)
-----------------------------------------------------------------
And I'm inserting like this
而我正在这样插入
def ins(request):
t =random.randint(0, 1000)
p = Q(a = t , b=0 , c=0)
p.save()
return HttpResponse('Saved')
Just curious what the hell is happening here
只是好奇这里到底发生了什么
Side note: If I set e.unique = False
I get 2-3 with the same e values before I get a new UUID values
旁注:如果我设置e.unique = False,在得到新的UUID值之前,我得到2-3具有相同的e值
1 个解决方案
#1
10
You should not call the function that you are passing to default
:
您不应该调用要传递给默认值的函数:
e = models.CharField(max_length=18, default=f, unique=True)
FYI, according to docs, you should pass a value or a callable:
根据文档,你应该传递一个值或一个可调用的值:
The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
字段的默认值。这可以是值或可调用对象。如果可调用,则每次创建新对象时都会调用它。
#1
10
You should not call the function that you are passing to default
:
您不应该调用要传递给默认值的函数:
e = models.CharField(max_length=18, default=f, unique=True)
FYI, according to docs, you should pass a value or a callable:
根据文档,你应该传递一个值或一个可调用的值:
The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
字段的默认值。这可以是值或可调用对象。如果可调用,则每次创建新对象时都会调用它。