为什么我的Land
对象没有保存?不要显示任何错误。
网址:
url(r'^add-object/(?P<id>\d+)/$','app.views.add_object'),
意见:
def add_object(request,id):
a = Animal.objects.get(id=id)
form = LandForm(request.POST, instance=a)
if request.method == 'POST':
if form.is_valid():
form.animal = a.id
form.save()
else:
form = LandForm(instance=a)
return render_to_response('add_object.html',{'form':form,},context_instance=RequestContext(request))
型号:
class Animal(models.Model):
name = models.CharField(max_length=255)
class Land(models.Model):
animal = models.ForeignKey(Animal)
表格:
class LandForm(ModelForm):
class Meta:
model = Land