Django:如何随机化每个现有模型的选择字段

时间:2018-11-07 12:16:02

标签: python django

我有100个Model实例,但是现在我添加了一个带有默认值的新选择字段。它正在工作,并且每个实例共享相同的字段,但是我希望它在选项的X值之间随机分配。

这是我的模型的修改版本

class MyModel(models.Model):

    A = 'a'
    B = 'b'
    C = 'c'

    CATEGORIES_CHOICES = (
        (A, 'Ant'),
        (B, 'Buffalo'),
        (C, 'Cat'),
    )
    category = models.CharField(max_length=1, choices=CATEGORIES_CHOICES, default=A)

这样我就可以进入shell并输入以下内容:

mymodel = MyModel.objects.get(id=1)
mymodel.category = random.choices(MyModel.CATEGORIES_CHOICES)[0][0]
mymodel.save()

它可以工作,但是我可以在所有100个实例中将其自动化吗?

1 个答案:

答案 0 :(得分:1)

如果是1次操作,只需进行for循环

如果不尝试这样的事情

DELIMITER $$

CREATE DEFINER=`master`@`%` PROCEDURE `spGetData`(
    IN  DisplayStart int ,
    IN  SortCol int ,
    IN  SortDir nvarchar(10),
    IN  Search  nvarchar(255)
)
BEGIN

        DECLARE FirstRec int;
        DECLARE LastRec int;

        SET FirstRec = DisplayStart;

            select 
               col1,col2,col3,col4,col6,col7,col8,col9,col10,col11
               col12,col13,col14,col15,col16,col17,col18,col19,col20
               col21,
                ( select count(*) from myTable) as filtered

            from myTable



order by
case When (@SortCol = 0 and @SortDir = 'asc')  then col1 end asc ,
case When (@SortCol = 0 and @SortDir = 'desc') then col1 end desc ,

case When (@SortCol = 1 and @SortDir = 'asc')  then col2 end asc ,
case When (@SortCol = 1 and @SortDir = 'desc') then col2 end desc ,

case When (@SortCol = 2 and @SortDir = 'asc')  then col3 end asc ,
case When (@SortCol = 2 and @SortDir = 'desc') then col3 end desc ,

case When (@SortCol = 3 and @SortDir = 'asc')  then col4 end asc ,
case When (@SortCol = 3 and @SortDir = 'desc') then col4 end desc ,

case When (@SortCol = 4 and @SortDir = 'asc')  then col5 end asc ,
case When (@SortCol = 4 and @SortDir = 'desc') then col5 end desc ,

case When (@SortCol = 5 and @SortDir = 'asc')  then col6 end asc ,
case When (@SortCol = 5 and @SortDir = 'desc') then col6 end desc ,

case When (@SortCol = 6 and @SortDir = 'asc')  then col7 end asc ,
case When (@SortCol = 6 and @SortDir = 'desc') then col7 end desc ,

case When (@SortCol = 7 and @SortDir = 'asc')  then col8 end asc ,
case When (@SortCol = 7 and @SortDir = 'desc') then col8 end desc ,

case When (@SortCol = 8 and @SortDir = 'asc')  then col9 end asc ,
case When (@SortCol = 8 and @SortDir = 'desc') then col9 end desc ,

case When (@SortCol = 9 and @SortDir = 'asc')  then col10 end asc ,
case When (@SortCol = 9 and @SortDir = 'desc') then col10 end desc ,

case When (@SortCol = 10 and @SortDir = 'asc')  then col11 end asc ,
case When (@SortCol = 10 and @SortDir = 'desc') then col11 end desc ,

case When (@SortCol = 11 and @SortDir = 'asc')  then col12 end asc ,
case When (@SortCol = 11 and @SortDir = 'desc') then col12 end desc ,

case When (@SortCol = 12 and @SortDir = 'asc')  then col13 end asc ,
case When (@SortCol = 12 and @SortDir = 'desc') then col13 end desc ,

case When (@SortCol = 13 and @SortDir = 'asc')  then col14 end asc ,
case When (@SortCol = 13 and @SortDir = 'desc') then col14 end desc ,

case When (@SortCol = 14 and @SortDir = 'asc')  then col15 end asc ,
case When (@SortCol = 14 and @SortDir = 'desc') then col15 end desc ,

case When (@SortCol = 15 and @SortDir = 'asc')  then col16 end asc ,
case When (@SortCol = 15 and @SortDir = 'desc') then col16 end desc ,

case When (@SortCol = 16 and @SortDir = 'asc')  then col17 end asc ,
case When (@SortCol = 16 and @SortDir = 'desc') then col17 end desc ,

case When (@SortCol = 17 and @SortDir = 'asc')  then col18 end asc ,
case When (@SortCol = 17 and @SortDir = 'desc') then col18 end desc ,

case When (@SortCol = 18 and @SortDir = 'asc')  then col19 end asc ,
case When (@SortCol = 18 and @SortDir = 'desc') then col19 end desc ,

case When (@SortCol = 19 and @SortDir = 'asc')  then col20 end asc ,
case When (@SortCol = 19 and @SortDir = 'desc') then col20 end desc ,

case When (@SortCol = 20 and @SortDir = 'asc')  then col21 end asc ,
case When (@SortCol = 20 and @SortDir = 'desc') then col21 end desc ,


                limit FirstRec,10;

    END

使用此解决方案,每次您将“无模型”保存为类别时,都会在保存之前设置一个随机类别。

但这不是实现数据一致性的最佳解决方案