python - How do I pass a parent id as an fk to child object's ModelForm using generic class-based views in Django? -
I am trying to use the Django Generic Class-Based Views to create the CRUD interface for two model databases. I have a working CRUD interface for parents model, and I am trying to start working on the child. For stability with other Django instances, keep parents and children as a book. What is the easiest way to allow users to add books to books?
In HTML words, I think I want to create a link on the author details page that contains the author's ID, that ID has been preset on the book form, and after that the book form processing Use that ID as the PK of that book But I do not understand how to use the DJGengo. I have read, and, each of which answers a little different question.
models.py
Category Author (models.Model): Name = Model. Serifield (max_long = 100) class book (model.model): author = model.Argink's (author) title = model.carfield (max_long = 500)
view Py forms.py urls.py templates / map / author_district 1 Template / myapp / book_form.html But I do not understand how to extract that variable ... context? ... and put it in the form. 1a) Can I make URL parameters instead of URL, i.e., 2) Once the variable is in the form, how can it be in the form of a secret input, so that the user will not be able to see it. ?
class BookForm (forms.Modelform): class meta: model = book
url (r '^ (? P & lt; pk & gt; \ d +) / $', author. Read_as_view (), Name = 'author_read'), URL (r 'book / create / (? P & lt; author_id & Gt; \ d +) / $ ', BookCreate.as_view (), name =' book_create '),
... & lt; P & gt; & Lt; A href = "{% url 'myapp: book_create' author_id = Author.pk%}" & Gt; Add a book & lt; / A & gt; & Lt; / P & gt; ...
& lt; Form action = "" method = "post" & gt; {% Csrf_token%} {{form.as_p}} & lt; Input type = "submit" value = "finished" & gt; & Lt; / Form & gt; 1) How do I get the author's form from the book page URL to the author, and then process it correctly? With the sample code above, the Django debugger shows that it exists in this way:
View arguments Arguments Argument URL name myapp.views.BookCreate () {'author_id': u'1234 '} Book_create
book / create? Replace Author = 1234
book / create / 1234 /
? Or even make the whole thing a post so that it is not part of the URL? Which is the best practice and how is it done?
with the URL that you defined in author_detail.html
author_id variable Will be accessible in self.kwargs ['author_id']
# views.py class BookCreate (CreateView) ... ... def form_valid (Self, Form): book = form.save (commit = false) author_id = form.data ['author_id'] Author = get_object_or_404 (author, id = author_id) book.author = author return super (BookCreate, self) .form_valid (Form) ... def get_context_data (self, ** kwargs): reference = super (BookCreate, itself) .get_context_data (** kwargs) reference ['a_id'] = self.kwargs ['author_id'] return reference
then your In the form you can add:
class BookForm (forms.Modelform): ... def __init __ (self, * arg, ** kwargs): self.fields ["author_id"] = Forms.CharField (widget = forms.HiddenInput ()) Super (BookForm, itself) .__ init __ (auto, * args, ** kwargs)
Then in the template:
& lt; Input type = hidden name = "author_id" value = "{{a_id}}" gt;
Should retrieve the form_valid id in the view, get the appropriate author and set that author as the author of books. commit = false
prevents the model that you already save and calling form.save (commit = true)
while the author is calling.
Comments
Post a Comment