请,我有一个类似的案例,我会介绍他们,并希望你能帮助我理解和正确处理这个案子。
我有简单的模型案例:
# -*- coding: utf-8 -*-
from django.db import models
class City(models.Model):
name = models.CharField("City", max_length=100, blank=False, null=False)
state = models.CharField("State", max_length=2, blank=False, null=False)
class Neighborhood(models.Model):
name = models.CharField("Name", max_length=100, blank=False, null=False)
city = models.ForeignKey(City, blank=False, null=False)
模型形式:
from django import forms
from app.models import *
class CityForm(forms.ModelForm):
class Meta:
model = City
class NeighborhoodForm(forms.ModelForm):
class Meta:
model = Neighborhood
state = forms.CharField("State", max_length=2, required=True)
他们的意见:
城市景观:
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from app.forms import CityForm
from app.models import City
STATES = ["AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PR",
"PB", "PA", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SE", "SP", "TO"]
def index(request):
if "submit" in request.POST:
form = CityForm(request.POST, request.FILES)
if form.is_valid():
form.save(commit=True)
elif "cancel" in request.POST:
return HttpResponseRedirect("/")
else:
form = CityForm()
cities = City.objects.all()
data = {
"form": form,
"states": STATES,
"cities": cities
}
return render_to_response("city/index.html", data, context_instance=RequestContext(request))
邻居观点:
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from app.forms import NeighborhoodForm
from app.models import Neighborhood, City
from app.city.views import STATES
def index(request):
if "submit" in request.POST:
form = NeighborhoodForm(request.POST, request.FILES)
if form.is_valid():
form.save(commit=True)
elif "cancel" in request.POST:
return HttpResponseRedirect("/")
else:
form = NeighborhoodForm()
neighborhoods = Neighborhood.objects.all()
cities = City.objects.all()
data = {
"form": form,
"states": STATES,
"cities": cities,
"neighborhoods": neighborhoods
}
return render_to_response("neighborhood/inserir.html", data, context_instance=RequestContext(request))
最后,邻居的模板:
{% extends "base.html" %}
{% block content %}
<form action="" method="post" id="neighborhoodForm" name="neighborhoodForm">
{% csrf_token %}
<div>
<label>State:</label>
<select id="state" name="state" autofocus="autofocus">
<option value=""></option>
{% for item in states %}
<option value="{{ item }}"
{% if item == form.state.value %}
selected="selected"
{% endif %}>{{ item }}</option>
{% endfor %}
</select>
</div>
<div>
<label>City:</label>
<select id="city" name="city">
<option value=""></option>
{% for item in cities %}
<option value="{{ item.id }}"
{% if item.id == form.city.value|add:0 %}
selected="selected"
{% endif %}>{{ item.name }}</option>
{% endfor %}
</select>
</div>
<div>
<label>Neighborhood Name:</label>
<input type="text" id="name" name="name" value="{{ form.name.value|default_if_none:"" }}"/>
</div>
<div>
<button type="submit" id="submit" name="submit" value="submit">Submit</button>
<button type="submit" id="cancel" name="cancel" value="cancel">Cancel</button>
</div>
</form>
<br/>
<table border="1">
<tr>
<th>Neighborhood Name</th>
<th>City</th>
<th>State</th>
</tr>
{% for item in neighborhoods %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.city.name }}</td>
<td>{{ item.city.state }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
考虑到社区,我遇到了困难。我有City和State字段,它们位于邻居模板中,用于过滤。
要添加记录我没有问题,但是一个简单的帖子不会再次将NeighborhoodForm的“状态”中的值返回给模板。那是因为帖子中发送的值没有找到表单的“状态”字段。
当您打开编辑记录时发生相同的事情,即“状态”字段将不会被填充。
所以这是我的问题。你能帮助我吗?我该怎么做或我做错了什么? 感谢所有人给予的帮助。
答案 0 :(得分:0)
您真正想要的是为字段分配和保存自定义值。为此,您必须提供initial values和save
表单方法。
如果您有一个要从中加载数据的对象,则必须将其传递给表单,例如:
neighbor = Neighborhood.objects.get(pk=1)
form = NeighborhoodForm(instance=neighbor)
上面的代码使用与之相关的对象和字段初始化表单。但它仍然错过state
字段。要初始化它,您必须为它传递初始值:
neighbor = Neighborhood.objects.get(pk=1)
state = neighbor.city.state
form = NeighborhoodForm(instance=neighbor, initial={'state': state})
或者您可以覆盖表单的__init__
方法来提取值:
def __init__(self, *args, **kwargs):
super(NeighborhoodForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs:
state = self.instance.city.state
self.fields['state'].initial = state
您可以通过覆盖保存方法来保存数据:
def save(self, *args, **kwargs):
new_neighbor = super(NeighborhoodForm, self).save(*args, **kwargs)
city = City.objects.create(state=self.cleaned_data['state'])
new_neighbor.city = city
new_neighbor.save()
return new_neighbor