1

我正在尝试创建一个视图,用户可以在其中通过模板中的表单编辑数据库记录。我搜索了很多网页(以及 Django 文档),他们在其中教授如何制作这些视图,但他们总是使用 Django 为每个模型生成的“id”。在这个特定的模型中,我必须使用 AutoField 来覆盖“id”。有没有办法使用这个 AutoField 作为 Django 记录的“id”?

这是我的完整模型:

class T031003 (models.Model): 
   C003IDCD = AutoField(primary_key=True)
   C003INST = models.IntegerField(unique=True) #usar AutoSlug
   C003TPCD = models.CharField(max_length=1)
   C003CHCD = models.CharField(max_length=14)
   C003MTR = models.CharField(max_length=30, blank=True, null=True)
   C003CTCD = models.CharField(max_length=3)
   C003RZSC = models.CharField(max_length=60, blank=True, null=True)
   C003EML = models.EmailField(max_length = 254, blank=True, null=True)
   C003LOGA = models.CharField(max_length=20)
   C003LOGB = models.DateTimeField()
   C003LOGD = models.CharField(max_length=15, blank=True, null=True)
   C003LOGF = models.CharField(max_length=20, blank=True, null=True)

   def __unicode__(self):
       return '%s' %  self.C003MTR

   class T031003Form(ModelForm):
       class Meta:
          model = T031003
          ordering = ["-C003MTR"]
          exclude = ('C003LOGA','C003LOGB','C003LOGD','C003LOGE','C003LOGF')

这是我试图做的视图,但它给了我错误“没有 T031003 匹配给定的查询。” 没错,因为表中没有“id”:

def t031003form_edit(request, id=None):
pin = get_object_or_404(T031003, pk=id)
form = T031003Form(request.POST or None, instance=pin)
if request.method == 'POST':
    if form.is_valid():
        form = form.save(False)
        form.C003LOGA = request.user
        form.C003LOGB = datetime.date.today()
        form.C003LOGD = request.META['REMOTE_ADDR']
        form.C003LOGF = request.META['USERDOMAIN']
        form.save()
        form = T031003Form()
    else:
        return HttpResponseRedirect('/erro/')
return render_to_response('T031003Form_edit.html', {'form': form,}, context_instance=RequestContext(request))

任何帮助将不胜感激!

4

2 回答 2

0

如果一个模型有一个 AutoField——一个自动递增的主键——那么这个自动递增的值将在你第一次调用 save() 时被计算并保存为你的对象的一个​​属性:

>>> b2 = Blog(name='Cheddar Talk', tagline='Thoughts on cheese.')
>>> b2.id     # Returns None, because b doesn't have an ID yet.
>>> b2.save()
>>> b2.id     # Returns the ID of your new object.

在调用 save() 之前,无法判断 ID 的值是多少,因为该值是由您的数据库计算的,而不是由 Django 计算的。

参考:https ://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs

于 2012-06-12T16:51:37.377 回答
0

好吧,多亏了一位密友的帮助,我可以使用表单集来解决问题。这是视图:

def t031002form_edit(request, id_auto):
j = get_object_or_404(T031002, pk=id_auto)

T031003FormSet = modelformset_factory(T031002, can_delete=True, max_num=1)

if request.method == 'POST':
    form = T031002FormSet(request.POST or None, request.FILES or None, queryset=T031002.objects.filter(pk=id_auto)) 
    if form.is_valid():
        instance = form.save(commit=False)
        form.C003LOGA = request.user
        form.C003LOGB = datetime.date.today()
        form.C003LOGD = request.META['REMOTE_ADDR']
        form.C003LOGF = request.META['USERDOMAIN']
        for reform in instance:
            reform.save()
    else:
       return HttpResponseRedirect('/erro/')
else:
    form = T031002FormSet(queryset=T031002.objects.filter(pk=id_auto))  
return render_to_response(('T031002Form_edit.html'), {'form': form,}, context_instance=RequestContext(request))

因此,使用表单集,您可以轻松无忧地工作。希望它可以帮助其他人解决同样的问题。

于 2012-06-17T07:37:29.650 回答