Django ModelChoiceField - 使用id以外的其他东西? [英] Django ModelChoiceField - use something other than id?

查看:502
本文介绍了Django ModelChoiceField - 使用id以外的其他东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个地址表,它有一个 postal_code 字段 - ModelChoiceField不允许我使用PK以外的其他东西验证存在正确吗?要走什么路?正常输入和使用 clean _ *()

解决方案

code> to_field_name ?我不知道它是否在任何地方记录,但是您可以在 ModelChoiceField 构造函数参数之间轻松找到它:https://github.com/django/django/blob/master/django/forms/models.py 。它用于过滤字段查询。



例如:

  article = ModelChoiceField(queryset = Articles.objects.all(),
to_field_name ='slug')


Say I have an address table and it has a postal_code field -- ModelChoiceField does not allow me to use something other than PKs to validate existence correct? What would be the way to go? Normal input and use clean_*()?

解决方案

What about to_field_name? I'm not sure if it's documented anywhere, but you can find it easily between ModelChoiceField constructor params: https://github.com/django/django/blob/master/django/forms/models.py. It is used to filter field queryset.

For example:

articles = ModelChoiceField(queryset=Articles.objects.all(),
        to_field_name='slug')

这篇关于Django ModelChoiceField - 使用id以外的其他东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆