0

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?

linbo
  • 2,393
  • 3
  • 22
  • 45

1 Answers1

1

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.

mariodev
  • 13,928
  • 3
  • 49
  • 61
  • yup, it works. But possible don't put desc field in input element? – linbo Dec 08 '13 at 13:55
  • @linbo I'm not sure what you're asking.. if you want to display it as text input, just create custom input tag and put `{{ form.instance.desc }}` as value. Just don't set any name attribute for the input, so it won't be picked up by request. – mariodev Dec 08 '13 at 14:06
  • want to put it in other element, for example in div element, not for input element – linbo Dec 08 '13 at 14:19
  • 1
    @linbo remove `desc` field from `include` attribute – mariodev Dec 08 '13 at 14:33