简体   繁体   English

如何在同一Django模型的另一个值中使用模型的值

[英]How to use the value of a model in another value of the same django model

I have a model Group that has a field maximum_capacity and a member field, what i want to do is to use the value the user enters in the field maximum capacity to set the max_choices for the member field. 我有一个模型组,该模型组具有一个字段maximum_capacity和一个成员字段,我要做的是使用用户在该字段的最大容量中输入的值来为该成员字段设置max_choices。

I am using Django2.2 and the max_choices option came from the MutiSelectField package i installed. 我正在使用Django2.2,max_choices选项来自我安装的MutiSelectField软件包。 I have tried converting to int using int() but gives the same error. 我尝试使用int()转换为int,但给出了相同的错误。

class Group(models.Model):
    number_of_group_members = ((i, i) for i in range(2, 100))
    members_choices = [(member.email, member.first_name + member.last_name)
                       for member in CustomUser.objects.all()]

    maximum_capacity = models.IntegerField(choices=number_of_group_members, 
        default='Specify group limit')
    members = MultiSelectField(max_choices=(i for i in 
        range(maximum_capacity)), unique=True, choices=members_choices)

I keep getting the Error: 我不断收到错误消息:

members = MultiSelectField(max_choices=(i for i in range(maximum_capacity)),
TypeError: 'IntegerField' object cannot be interpreted as an integer

One problem here may be be that your IntegerField default is a string value and that string could be causing trouble. 这里的一个问题可能是您的IntegerField默认值是一个字符串值,并且该字符串可能会引起麻烦。 Anyway, to help you properly you should post more code. 无论如何,为了正确帮助您,您应该发布更多代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django 设置模型值,使用相同模型的另一个值 - Django set model value, using another value of the same model 如何减去 django model 中的两个 DateTime 字段并将减去的值保留在同一 model 的另一个字段中? - how to subtract two DateTime field in django model and keep the subtracted value in another field in same model? Django如何从另一个模型获取模型中的值 - Django how to get in model the value from another model 如何基于Django中的属性值将模型实例复制到另一个模型 - How to copy a model instance to another model based on attribute value in Django 使用模型方法作为Django Model的默认值? - Use model method as default value for Django Model? 在Django中使用具有相同值的不同模型字段 - Use different model's field with the same value in Django 根据此 model django 中的另一个值自动更新 model 值 - Auto update model value based on another value in this model django 将 model 中的一个字段的值设置为 django 中另一个 model 中的一个字段的值 - Set the value of a field in a model as the value of a field in another model in django Django:将新外键添加到现有模型中,并将默认值作为同一模型中的另一个外键 - Django: Add new foreign key to existing model with default value as another foreign key from the same model 如何基于同一模型中其他字段的值设置Django模型字段值 - How to set django model field value based-on value of other field in the same Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM