Search code examples
pythondjango-modelsdjango-formsautofield

Django: Edit ModelForm using AutoField


I'm trying to make a view where the user can edit DB records through a form in a template. I've searched a lot of web pages (and Django docs as well) where they teach how to make these views, but they always use the "id" that Django generates for each Model. In this particular Model, I have to use an AutoField to override the "id". Is there a way to use this AutoField as an "id" of the record with Django?

Here's my complete model:

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')

And here's the view I tried to do, but it gives me the error "No T031003 matches the given query." and it's right, since there is no "id" in the table:

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))

Any help would be very appreciated!


Solution

  • Well, thanks to the help from a close friend, I could do the trick using formsets. Here's the view:

    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))
    

    So, with formsets, you can work nicely and with no worries. Hope it helps others with this same questioning.