Question

models.py

class Application(models.Model):
    desc = models.CharField(max_length=40, unique=True, null=False)
    name = models.CharField(max_length=40, null=False)
    user = models.ForeignKey(User)

views.py

class ApplicationUpdate(UpdateView):
    model = Application
    template_name = "apps/update.html"
    fields = ['name', 'desc']
    context_object_name = "app"
    success_url = reverse_lazy('app-list')

templates

    <div class='update-item'>
        {{form.name.error}}
        {{form.name.label_tag}}
        {{form.name}}
    </div>
    <div class='update-item'>
        {{form.desc.value}}
    </div>

Here I want to display desc field in template, but only POST name field when update it. Any solution for it?

Was it helpful?

Solution

Try one of these:

  1. Set readonly attributes on fields that you don't want to be changed.

  2. Instead of {{form.desc.value}} you can display instance field value - {{ form.instance.desc }}, next remove desc field from include attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top