I am trying Edit/Update to create a model object (record) using django Updateview
model.py
from django.db import models from myapp.models import Author class Book(models.Model): author = models.ForeignKey(Author) book_name = models.CharField(max_length=260) amount = models.DecimalField( max_digits=10, decimal_places=2, default=0.00) description = models.TextField()
views.py
class BookEditView(generic.UpdateView): model = Book template_name_suffix = '_update_form' exclude = ('author',) def get_success_url(self): return reverse("books:list_of_books")
books_update_from.html
{% extends "base.html" %} {% block main_title %}Edit Book{% endblock %} {% block content %} <form method="post" action="" class="form-horizontal"> {{form.as_p}} </form> {% endblock %}
When the form is displayed, the external Author field is still displayed on the page, although I mentioned this in the exclude in BookEditview as above
So how to hide this field and submit the form?
I also tried to display individual fields like form.book_name , form.amount , etc. (I know that this approach does not solve the above problem, but just gives a stupid attempt). when I submit a form, its action is careless and does nothing, because we are not sending author foreign key value and therefore the form is invalid and submit does nothing
So what I want to know is how to exclude a field from the model when rendering it as a form using Updateview to edit the model (did I do something wrong in my code?)
I also want to know the concept of Updateview , that if we exclude the foreignKey field, then there is no need to enter the value of the foreign key? (Since the Edit form will only be valid if every field, including Author Foreign Key sent with a value?)