# Modify only one field of the model

**URL:** <https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501>\
**Category:** Forms & APIs\
**Created:** [March 14, 2023, 10:36am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501 "2023-03-14T10:36:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 14, 2023, 10:36am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/1 "2023-03-14T10:36:58Z")

</div>

I am trying to modify only one field of my modal. For this I have copied the class where I edit the record, but of course, it tells me that there are mandatory fields that I must put.  
Is there a way to avoid that and only modify the field that I want?  
views.py

```auto
class AprobarReclamacion(UpdateView):
    model = Rec
    form_class = RecForm
    template_name = 'reclamaciones/aprobar_reclamacion.html'
    
    def post(self,request,*args,**kwargs):
        request.headers.get('x-requested-with') == 'XMLHttpRequest'
        if request.headers.get('x-requested-with') == 'XMLHttpRequest':
            form = self.form_class(request.POST,instance = self.get_object())
            if form.is_valid():
                nueva_reclamacion = Rec(
                    estado_cod_id = 5,
                )
                form.save()
                mensaje = f'{self.model. __name__ } actualizado correctamente!'
                error = 'No hay error!'
                response = JsonResponse({'mensaje': mensaje, 'error': error})
                response.status_code = 201
                return response
            else:
                mensaje = f'{self.model. __name__ } no se ha podido actualizar!'
                error = form.errors
                response = JsonResponse({'mensaje': mensaje, 'error': error})
                response.status_code = 400
                return response
        else:
            return redirect('inicio_reclamaciones')

```

The field I want to modify is estado\_cod\_id.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2023, 11:32am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/2 "2023-03-14T11:32:29Z")

</div>

How do you want the other fields to appear?

- Do you even want those other fields to appear?
- Or do you want them to be shown, but be read-only?

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 14, 2023, 11:49am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/3 "2023-03-14T11:49:41Z")

</div>

I don’t want them to be displayed since the change of state is done by someone else who simply “authorizes”, that’s why the state changes.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2023, 11:52am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/4 "2023-03-14T11:52:46Z")

</div>

Then I would create a new form (not a model form) with whatever widget you want to use to select the new state, and use the [`update`](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#update) method on the object to change that one value of that model.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 14, 2023, 1:19pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/5 "2023-03-14T13:19:17Z")

</div>

> [@KenWhitesell](#):
>
> Then I would create a new form (not a model form)

Do you mean create a new form in forms.py?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2023, 1:21pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/6 "2023-03-14T13:21:06Z")

</div>

Yes, along with the appropriate view to handle it.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 14, 2023, 1:39pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/7 "2023-03-14T13:39:15Z")

</div>

I get the following:

```auto
ValueError: Cannot assign "'5'": "Rec.estado_cod" must be a "Sat_estados" instance.

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2023, 1:40pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/8 "2023-03-14T13:40:41Z")

</div>

I need to see all the code related to this - the form and the view.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 14, 2023, 1:46pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/9 "2023-03-14T13:46:01Z")

</div>

views.py

```auto
class AprobarReclamacion(UpdateView):
    model = Rec
    form_class = RecFormCanvioEstado
    template_name = 'reclamaciones/aprobar_reclamacion.html'
    
    def post(self,request,*args,**kwargs):
        request.headers.get('x-requested-with') == 'XMLHttpRequest'
        if request.headers.get('x-requested-with') == 'XMLHttpRequest':
            form = self.form_class(request.POST,instance = self.get_object())
            if form.is_valid():
                nueva_reclamacion = Rec(
                    estado_cod = 5,
                )
                form.save()
                mensaje = f'{self.model. __name__ } actualizado correctamente!'
                error = 'No hay error!'
                response = JsonResponse({'mensaje': mensaje, 'error': error})
                response.status_code = 201
                return response
            else:
                mensaje = f'{self.model. __name__ } no se ha podido actualizar!'
                error = form.errors
                response = JsonResponse({'mensaje': mensaje, 'error': error})
                response.status_code = 400
                return response
        else:
            return redirect('inicio_reclamaciones')

```

forms.py

```auto
class RecFormCanvioEstado(forms.ModelForm):
    class Meta:
        model = Rec
        fields = ['estado_cod']

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2023, 1:51pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/10 "2023-03-14T13:51:37Z")

</div>

Side note: This following line doesn’t do anything for you and can be removed

> [@peya02](#):
>
> `request.headers.get('x-requested-with') == 'XMLHttpRequest'`

Side note: This also doesn’t appear to have any use in this code and can be removed.

> [@peya02](#):
>
> ```auto
> nueva_reclamacion = Rec(
> estado_cod = 5,
> )
> 
> ```

Also, from an earlier reply:

> [@KenWhitesell](#):
>
> Then I would create a new form **(not a model form)**

[Emphasis added]

You want this to be a regular form with just that one field, not a model form.

Then, when that form is submitted:

> [@KenWhitesell](#):
>
> use the [`update`](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#update) method on the object to change that one value of that model

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 15, 2023, 6:27am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/11 "2023-03-15T06:27:58Z")

</div>

I’m a bit lost with “You want this to be a regular form with just that field, not a template form.”  
That’s why I asked earlier if I should create another form in forms.py because I wasn’t very clear about what you’re saying about the regular form.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 15, 2023, 7:29am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/12 "2023-03-15T07:29:52Z")

</div>

You mean something like this:

```auto
class RecFormCanvioEstado(forms.Form):
    estado_cod = forms.TextInput(attrs = {
                    'class': 'form-control',
                    'placeholder': 'Debe aprobar',
                    'readonly':'readonly',
                    })

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 15, 2023, 11:21am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/13 "2023-03-15T11:21:53Z")

</div>

I’m wondering if we’re dealing with a translation issue.

I did not say “not a template form”, I said to not use a model form. In other words, your form should subclass `Form`, not `ModelForm`.

Also, I think your other posts identify that the `estado_cod` field is a ForeignKey, which means your most appropriate field is going to be a [`ModelChoiceField`](https://docs.djangoproject.com/en/4.1/ref/forms/fields/#modelchoicefield).

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 15, 2023, 12:16pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/14 "2023-03-15T12:16:23Z")

</div>

We may not be understanding each other due to the language change…  
I’ll explain a bit what I want to do.  
As you comment, I have a ForeignKey field that is estado\_cod which puts the value according to another table called sat\_estados  
What I want is to be able to modify that estado\_cod value by clicking on a button so that it goes from one state to another but without having to fill in any form.

On the other hand, I think that the subclass form theme is what I was presenting to you before:

```auto
class RecFormCanvioEstado(forms.Form):
    estado_cod = forms.TextInput(attrs = {
                    'class': 'form-control',
                    'placeholder': 'Debe aprobar',
                    'readonly':'readonly',
                    })

```

Although I don’t know how to apply it to my view class ApprobarReclamacion(UpdateView):  
And pass it an Update()  
I don’t know if I understood better.  
Thank you.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 15, 2023, 12:35pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/15 "2023-03-15T12:35:19Z")

</div>

> [@peya02](#):
>
> What I want is to be able to modify that estado\_cod value by clicking on a button so that it goes from one state to another but without having to fill in any form.

Ok, this is different from everything we’ve talked about before - and in some ways a lot easier.

There are a couple different ways to do this, what I’m outlining below is just one way - and it’s a method we use.

- Define a view that:

- Define a url for that view with a url parameter for the pk

On the page where you want this action to be triggered:

- Define an `<a>` tag to that new url. You can use a `<button type="button">` inside the `<a>` and `</a>` tags so that it looks like a button instead of a normal link.

Now, if you choose to continue with using a form:

> [@peya02](#):
>
> Although I don’t know how to apply it to my view class ApprobarReclamacion(UpdateView):

You wouldn’t. You would be creating a new view to do that. If you want to use a CBV as the base, it would be a `FormView` for your new form, not an `UpdateView`.

> [@peya02](#):
>
> And pass it an Update()

You don’t “pass it an update”. Your `FormView` needs to get the object being updated, and you use the [`update`](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#update) method on that object to change it.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 15, 2023, 1:48pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/16 "2023-03-15T13:48:40Z")

</div>

I will go step by step.  
First, can I create the view based on a class or does it have to be def?  
To update I see that it would be something like this:

```auto
Rec.objects.filter(id=pk).update(estado_cod=5)

```

I’m good?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 15, 2023, 2:29pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/17 "2023-03-15T14:29:10Z")

</div>

> [@peya02](#):
>
> First, can I create the view based on a class or does it have to be def?

Either. Functionally, it doesn’t make a difference.

If you create a CBV for this, it would likely use `View` as the base class. There’s just not a lot of functionality in it.

As a result, if it were me, I’d create this as an FBV.

> [@peya02](#):
>
> `Rec.objects.filter(id=pk).update(estado_cod=5)`

Correct.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 16, 2023, 7:11am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/18 "2023-03-16T07:11:12Z")

</div>

Hello,  
I’m starting to get involved and I don’t really know how to continue, I think it’s the least complicated of all but I can’t get out of this.  
I have the following:

code views.py

```auto
def AprobarReclamacion(pk):
    
    Rec.objects.filter(id=pk).update(estado_cod=5)
    return redirect('inicio_reclamaciones')

```

urls.py code

```auto
path('reclamaciones/aprobar_reclamacion/<int:pk>/', AprobarReclamacion, name='aprobar_reclamacion'),

```

js code

Here in the js I am not very clear how to indicate the path to the link/button

```auto
function listadoReclamaciones(){
    $.ajax({
        url: "/reclamaciones/listado_reclamaciones/",
        type:"get",
        dataType: "json",
        success: function(response){
            if($.fn.DataTable.isDataTable('#tabla_reclamaciones')){
                $('#tabla_reclamaciones').DataTable().destroy();
            }
            $('#tabla_reclamaciones tbody').html("");
            for(let i = 0;i < response.length;i++){
                let fila = '<tr>';
                fila += '<td>' + (i +1) + '</td>';
                fila += '<td>' + response[i]["fields"]['num_reclamacion'] + '</td>';
                fila += '<td>' + response[i]["fields"]['motivo'] + '</td>';
                fila += '<td>' + response[i]["fields"]['fecha_reclamacion'] + '</td>';
                fila += '<td>' + response[i]["fields"]['aprueba'] + '</td>';
                fila += '<td>' + response[i]["fields"]['del_dep_code'] + '</td>';
                fila += '<td>' + response[i]["fields"]['rec.estado_cod.nombre'] + '</td>';
                fila += '<td><button type = "button" class = "btn btn-primary btn-sm tableButton" ';
                fila += 'onclick = "abrir_modal_edicion(\'/reclamaciones/actualizar_reclamacion/' + response[i]['pk']+'/\');"> EDITAR </button>';
                fila += '<a name="" id="" class="btn btn-primary" href="" role="button">APROBAR</a>';
                fila += '</td>';

```

Here in the js I am not very clear how to indicate the path to the link/button. The last part where it says APROBAR is where I don’t know.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2023, 11:30am UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/19 "2023-03-16T11:30:57Z")

</div>

> [@peya02](#):
>
> `url: "/reclamaciones/listado_reclamaciones/",`

You want to concatenate the desired pk to that url string. How you identify what pk to apply depends upon how you’re constructing the page and the links.

For example, you could define your function with a parameter:  
`function listadoReclamaciones(pk){`  
and then you could have:  
`url: "/reclamaciones/listado_reclamaciones/"+pk+"/",`

You could then define your buttons with the `onclick` attribute to call this function passing the pk for _that_ button as the parameter.

But however you choose to do it, it isn’t something done “automatically” - you need to write the code for this to happen.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/peya02/32/11900_2.png) [@peya02](https://forum.djangoproject.com/u/peya02)\
**Post date:** [March 16, 2023, 1:27pm UTC](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501/20 "2023-03-16T13:27:35Z")

</div>

The concept is clear on how to do it but the code gets stuck and I don’t know where to go.  
I have the def part

```auto
def AprobarReclamacion(pk):
    
    reclamacion = Rec.objects.filter(id=pk).update(estado_cod=5)
    return redirect('inicio_reclamaciones')

```

I don’t know if that is enough… on the urls side I have this

```auto
    path('reclamaciones/aprobar_reclamacion/<int:pk>/', AprobarReclamacion, name='aprobar_reclamacion'),

```

And I would be missing the theme of the button. Of course, I show the records with a DataTable as I already commented, so I need to add that button/link inside the js function (part that I don’t know how to do)

[Next page](https://forum.djangoproject.com/t/modify-only-one-field-of-the-model/19501.md?page=2)
