Saving form data rewrites the same row
我不知道如何保存表单数据创建新行,保存时只使用相同的"id"重写数据,并在多行时向我显示错误,这是我的代码:
型号.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Submitter(models.Model): submitter=models.ForeignKey(User) def __unicode__(self): return self.submitter.username class Store(models.Model): creator=models.ForeignKey(Submitter) name = models.CharField(_('name'),blank=True,max_length=30) st = models.CharField(_('Street'),blank=True,max_length=30) sub_date = models.DateField(_('Visit Date'),) def __str__(self): return u'%s-%s-%s'%(self.creator,self.name,self.sub_date) |
视图.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | def StoreSave(request): if request.method == 'POST': form = StoreForm(request.POST) if form.is_valid(): submitter, dummy= Creator.objects.get_or_create(creator=request.user) store, created = Store.objects.get_or_create( submitter=submitter ) store.name = form.cleaned_data['name'] store.st = form.cleaned_data['st'] store.visit_date = form.cleaned_data['visit_date'] store.save() return HttpResponseRedirect('/user/%s/' % request.user.username) else: form = StoreForm() variables = RequestContext(request, { 'form': form }) return render_to_response('store/create_store.html', variables) |
号
如果要创建新行,请创建新行。:-)像
1 2 3 4 5 | store = Store(submitter=submitter, name=form.cleaned_data['name'], st=form.cleaned_data['st'], store.visit_date=form.cleaned_data['visit_date']) store.save() |
现在,您使用
顺便说一下,最好将这个保存代码放在表单的方法中(例如
P.S.刚注意到你的模型中没有