Django - 将OneToOneField映射到管理界面上的单选按钮 [英] Django - Map a OneToOneField to radio buttons on the admin interface
问题描述
我有以下内容:
class A(models.Model):
true_b = models.OneToOneField('B', related_name='is_true')
def save(self, *args, **kwargs):
if self.true_b not in self.b_set.all():
raise ValueError('True_b has to be among those B that refer to this A')
super(A, self).save(*args, **kwargs)
class B(models.Model):
a = models.ForeignKey(A)
...
@property
def is_true(self):
return a.true_b == self
这将在管理界面中为 true_b生成一个下拉列表
具有全部 B
中的项目,验证将确保所选项目实际返回到 A
。
This will generate a drop down list in the admin interface for true_b
having all the items in B
, and the validation will make sure the selected item is actually referring back to A
.
而不是在管理界面中有一个下拉列表,我如何可以对每个条目有无线电栏 B
将映射所选项目到 true_b
value?
Instead of having a drop down list in the admin interface, how can I have radio fields against each entry in B
that will map the selected item to true_b
value?
这样,选择项目会更容易(而不是查找它的名称并从下拉列表中选择),用户将无法选择任何不属于 A
的项目。
This way it will be easier to choose the item (instead of looking up it's name and selecting from the drop down list), and the user will not be able to choose any item that does not belong to A
.
推荐答案
您可以为此模型创建自己的表单,并将其传递给管理类
You can create your own form for this model and pass it to admin class
class MyAdmin()
class Meta()
form=MyFormForAdmin
这篇关于Django - 将OneToOneField映射到管理界面上的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!