Thursday 15 March 2012

Forms in Django -


I have two models, studios and an agent. Because there is some area of ​​interest in the student model, I have a Form for ModelForm I am using and it is very good that every student is an agent:

  class students (models.model): ncode = models.CharField (MAX_LENGTH = 12) name = models.CharField ( MAX_LENGTH = 40) family = models.CharField (MAX_LENGTH = 60) father = models.CharField (MAX_LENGTH = 40) telephone = models.CharField (MAX_LENGTH = 18, empty = true) address = models.CharField (MAX_LENGTH = 256) reagent = Models.ForeignKey (Reagent) state = model. Degree of IntegerField (option = STUDYING_STATUS) = models.IntegerField (option = DEGREE_STATUS) class reagent (models.Model): name = models.CharField (MAX_LENGTH = 40) family = models.CharField (MAX_LENGTH = 60) telephone = models.CharField (MAX_LENGTH = 18)   

These forms are:

  class Student_Form (ModelForm): class meta model = student class Reagent_Form (ModelForm): Class Meta: Model = Reagent   

But I had a plan to get both agent and student in one form, so I put them in a form together in a template:

  & Lt; Form action = "" method = "post" & gt; {% Csrf_token%} & lt; Table & gt; {{Student_form.as_table}} {{reagent_form.as_table}} & lt; / Table & gt; & Lt; Input type = "submit" value = "add" & gt; & Lt; / Form & gt;   

My problem is how can I enter information in different instances of student and agent forms? If there is only one form in the template, then I F = StudentForm (Request.POST) ! But in this case the form has been mixed

you can still

  F = Student_Form (request.POST) r = Reagent_Form (request.POST)   

and Django will provide the appropriate fields.

To hide the FK area,

  class Student_Form (ModelForm): class meta model = student out = ('reagent', class) Reagent_Form (ModelForm): class Meta: Model = Reagent  
  def myview (request): reagent_form = Reagent_Form (prefix = 'reagent' Student_Form (prefix = 'student') if request.POST: reagent_form = Reagent_Form (request.POST , Prefix = 'reagent') one each one student_form = Student_Form (request.POST, prefix = 'student') if reagent_form.is_valid () and student_form.is_valid (): reagent = reagent_form.save () # First commodity student = Student_form.save (commit = false) student.reagent = reagent #then Allocate Atr Code of student.save () #rest    

No comments:

Post a Comment