简体   繁体   中英

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.

I am using Django2.2 and the max_choices option came from the MutiSelectField package i installed. I have tried converting to int using int() but gives the same error.

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. Anyway, to help you properly you should post more code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM