问题描述
我使用Django Admin建立管理网站。有两个表,一个是 ModelA
,其中包含数据,另一个是 ModelB
,其中没有任何内容。如果 ModelB
中的一个模型字段 b_b
为None,则可以显示在值为<$ c的网页上$ c> ModelA 的字段 a_b
。
I use Django Admin to build a management site. There are two tables, one is ModelA
with data in it, another is ModelB
with nothing in it. If one model field b_b
in ModelB
is None, it can be displayed on the webpage with the value of ModelA
's field a_b
.
我不知道要做到这一点,这是伪代码:
I don't know how to do it, Here is the pseudo code:
在models.py
In models.py
from django.db import models
class ModelA(models.Model):
a_a = models.CharField(max_length=6)
a_b = models.CharField(max_length=6)
class ModelB(models.Model):
modela = models.OneToOneField(ModelA)
b_b = models.CharField(max_length=6, choices=[(1, 'M'), (2, 'F')], default=self.modela.a_b)
在admin.py
from django.contrib import admin
from .models import ModelA, ModelB
class ModelBAdmin(admin.ModelAdmin):
fields = ['b_b']
list_display = ('b_b')
我想如果modelB的b_b的默认值为relative a_b value在modelA中,我在django管理员更改(编辑)页面中有一个默认值b_b(View),它将数据与特定的modelB实例相关联。
I think if b_b of modelB have default value of relative a_b value of modelA, I have a default value of b_b(View) in the django admin change(edit) page which associate data to a particular modelB instance.
我想在网页中更容易填写b_b。
I want to fill b_b in web page more easily.
我不知道这个要求是否可以实现?还有另一种方法可以实现这一点?
I don't know whether this requirement can be achieved? Or there is another way can achieve this?
我尝试自定义模板过滤器如下:
I try to custom the templates filter as following:
template / admin / includes / fieldset.html
In templates/admin/includes/fieldset.html
{% load drop_null %}
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
{% else %}
{{ field.label_tag }}
{% if field.is_readonly %}
<p>{{ field.contents }}</p>
{% else %}
{% if field.field.name == 'b_b' %}
{{ field|dropnull }}
{% else %}
{{ field.field }}
{% endif %}
{% endif %}
{% endif %}
在模板标签/ drop_null.py
In templatetags/drop_null.py
from django import template
register = template.Library()
@register.filter(name='dropnull')
def dropnull(cls):
return cls.modela.a_b
'field'是< django.contrib.admin.helpers.AdminField对象在0x3cc3550>。它不是ModelB的一个实例。
如何获取ModelB实例?
The 'field' is < django.contrib.admin.helpers.AdminField object at 0x3cc3550> . It is not an instance of ModelB.How can I get ModelB instance?
推荐答案
您可以覆盖 save modelB
模型的c $ c>方法,而不是提供默认值:
You can override the save
method of your modelB
model instead of providing a default value:
class ModelB(models.Model):
modela = models.OneToOneField(ModelA)
b_b = models.CharField(max_length=6, choices=[(1, 'M'), (2, 'F')], blank=True)
def save(self, *args, **kwargs):
if not self.b_b:
self.b_b = self.modela.a_b
super(ModelB, self).save(*args, **kwargs)
在评论中澄清后编辑
如果您只想向参观者显示价值,但想保留事实上, ModelB.b_b
是空的,逻辑应该放在模板中。
Edit after clarification in the comments
If you just want to display the value to visitors but want to keep the fact that the ModelB.b_b
is empty, the logic should lay in the template.
假设你的视图集 objB
在上下文中,您有兴趣显示的 ModelB
对象,相关模板的一部分应该如下所示:
Assuming your view set objB
in the context being the ModelB
object your are interested to display, the relevant part of the template should look like:
{% with objB.b_b as bValue %}
{% if not bValue %}
<p>{{ objB.modela.a_b }}</p>
{% else %}
<p>{{ bValue }}</p>
{% endif %}
{% endwith %}
这篇关于基于另一个模型字段的Django模型字段默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!