简体   繁体   中英

Django Choices based on Foreign Key

I am wondering if there is a way to use a list that is stored in a foreignkey as the choices for a charfield.

Here is what I have right now but it is not working.

priority = models.ForeignKey(SLAs)
prioritylevel = models.CharField(choices=priority.details)

It says that the ForeignKey has no attribute 'details'

If this has been answered, please point me the correct direction.

Thanks :)

I think you want use this in forms?

So you can do smth like this:

class MyForm(forms.ModelForm):
    prioritylevel = forms.ModelChoiceField(queryset=OtherModel.objects.values('level'))

and in model this will just CharField . It this solve your problem?

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