Django 将数据作为属性从数据库传递到表单中
我想用一些复选框呈现一个表单。这些复选框有一个带有附加信息的弹出框,这些信息存储在数据库中。 如何访问表单模板中的附加数据 在执行以下操作时,是否应该在视图中执行此操作:Django 将数据作为属性从数据库传递到表单中,django,django-forms,django-templates,popover,django-crispy-forms,Django,Django Forms,Django Templates,Popover,Django Crispy Forms,我想用一些复选框呈现一个表单。这些复选框有一个带有附加信息的弹出框,这些信息存储在数据库中。 如何访问表单模板中的附加数据 在执行以下操作时,是否应该在视图中执行此操作:form=SomeForm(此处是什么? 它是否属于表单模板内部 我在这里有点迷茫,也许我需要一个例子 models.py: class SomeModel(models.Model): name = models.CharField('Model name', unique=True, validators=[MinL
form=SomeForm(此处是什么?
它是否属于表单模板内部
我在这里有点迷茫,也许我需要一个例子
models.py:
class SomeModel(models.Model):
name = models.CharField('Model name', unique=True, validators=[MinLengthValidator(1)], max_length=200)
stuff = models.ForeignKey('Stuff',related_name='stuff')
def __str__(self):
return self.name
class Stuff(models.Model):
name = models.CharField('Stuffname', unique=True, validators=[MinLengthValidator(1)], max_length=200)
info = models.TextField('Info', validators=[MinLengthValidator(1)], max_length=300)
def __str__(self):
return self.name
forms.py:
class SomeForm(ModelForm):
stuff = ModelChoiceField(queryset=Stuff.objects.all(), empty_label=None)
def __init__(self, *args, **kwargs):
super(SomeForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.layout = Layout(
'name',
InlineCheckboxes('stuff'),
Submit('save', 'save')
)
class Meta:
model = Stuff
fields = '__all__'
CustomForm.html:
<div class="controls {{ field_class }}"{% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
{% include 'bootstrap3/layout/field_errors_block.html' %}
{% for choice in field.field.choices %}
{% if not inline_class %}<div class="checkbox">{% endif %}
<label class="checkbox-img {% if inline_class %}checkbox-{{ inline_class }}{% endif %}
data-toggle="popover" data-trigger="hover" data-placement="top" data-title="{{choice.0}}" data-html="true" data-content="How to get the data in here? ">
<input type="checkbox"{% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
</label>
{% if not inline_class %}</div>{% endif %}
{% endfor %}
{% include 'bootstrap3/layout/help_text.html' %}
<div class="controls {{ field_class }}"{% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
{% include 'bootstrap3/layout/field_errors_block.html' %}
{% for choice in field.field.queryset%}
{% if not inline_class %}<div class="checkbox">{% endif %}
<label class="checkbox-img {% if inline_class %}checkbox-{{ inline_class }}{% endif %}
data-toggle="popover" data-trigger="hover" data-placement="top" data-title="{{choice.name}}" data-html="true" data-content={{ choice.info }}">
<input type="checkbox"{% if choice.id in field.value or choice.id|stringformat:"s" in field.value or choice.id|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.id|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
</label>
{% if not inline_class %}</div>{% endif %}
{% endfor %}
{% include 'bootstrap3/layout/help_text.html' %}
看看解决方案。谢谢,你给了我正确的方向。Field.queryset是一个不错的选择。我必须使用field.field.queryset,我真的不明白为什么。。。但是对我有用:)看看解决方案。谢谢,你给了我正确的方向。Field.queryset是一个不错的选择。我必须使用field.field.queryset,我真的不明白为什么。。。但对我来说是有效的:)