Saturday, 15 January 2011

Saving to two models via intermediate form - Django -


OK guys, I'm stuck.

The problem I have is as follows: I have two models, relinvoice coding record and EventRecord .
The user should be able to choose whether any article is relevant. If so, then other fields of the form should be required, if it is not = there is no required field, it should be in any one form, on the same page, without JS.

I have done so far to create such a form (forms.Form) which has many fields of two models. The following article, I tried to apply the conditional required argument:

  class coding form (forms.Form): relevance = forms.BooleanField (required = false) event_date = Forms Datemetiled ('Date of Event', Required = Incorrect) Location = forms Model Chavlfild (Kweriset = Sthankobjektkol (), Rikt_labil = No, required = false) actor = forms Kkrifild (Adiktm_lambi = 100, required = false) Party = forms.NullBooleanField ( 'party') labor_org = forms. NullBooleanField ( 'ILO') property_dmg = forms.NullBooleanField ( 'property damage') violence = FormskNullBooleanField ( "violence") injured = deaths forms.IntegerField (required = false) = forms.IntegerField (expected = False) participants_num = forms.IntegerField (expected = false) participants_str = forms.IntegerField (expected = false) anti_gov = forms.NullBool eanField ( 'anti-government') presence = forms.NullBooleanField ( 'intervention in the presence') = forms.NullBooleanField ( 'intervention') toughness = forms.NullBooleanField ( 'cruelty') ar = F Orms.NullBooleanField ( 'arrest') Release = forms.CharField (max_length = 100, required = false) url = forms.CharField (max_length = 100, required = false) def __init __ (self, data = none, * args, ** Kwargs): super (coding form , Itself) .__ init __ (data, * args, ** kwargs) if data and data.get ('relevance', none) == Correct: self.fields ['event_date']. Essential = true self Field ['location']. Essential = true self Field ['anti-gov']. Essential = true   

In my opinion, I tried to get the form and pass it to the Relevance Coding Record and EventRecord Modelsform values ​​and save all of them:

  If request.method == 'POST': 'coding' in request.POST: event_form = EventRecordForm (request.POST, prefix = 'event' ) relevance_form = RelevanceCodingRecordForm (request.POST, prefix = 'event') coding_form = CodingForm (request.POST, prefix = 'coding') if coding_form.is_valid (): obj = coding_form.save (commit = false) event = event_form. Save (commit = false) relevance = relevan Ce_form.save (Commit = false) # Relevance Data Relevance Relevance = obj.relevance Relevance. True = true relevance Classified = true relevance Coder = request.user.coder Relevance News = pic. Page (page) .object_list [0] relevance LAST_UPDATED = datetime.datetime.today relevance.save () if relenvace.relevance == is true: # event data event.article = paginator.page (page) .object_list [0] event.event_date = obj.event_date event.country = paginator .page (page) .object_list [0] .country event.location = obj.event_location event.actors = obj.actors event.party = obj.party event.labor_org = obj Klabor_org Iventkproprti_dng = Objkproprti_dng Iventkwaylens = Objkwaylens Iventkinjured = Objkinjured Iventkdekts = Objkdots Iventkpartisipents_nm = Objkpartisipents_num Iventkpartisipents_str = Objkpartisipents_str Ivent.anti_gov = Obj. Anti_gov event.presence = obj.proenceense event.intervene = obj.tervene event.brutality = obj.butality event.arrests = obj.arrests event.issue = obj.issue event.url = pagator.page (page) .object_list [0 ] .source event.coder = request.us Er.coder event.last_updated = datetime.datetime.today event.save () redirect_to = "? Page =% s"% return page HttpResponseRedirect (redirect_to) Other: redirect_to = "? Page =% s "% return page HttpResponseRedirect (redirect_to) Other: coding_form = CodingForm (prefix = 'coding') coding_form.fields [" location "]. Queryset = Location.objects.filter (ccode = country) .order_by ('name ')   

This does not work, however: no records have been created in my database, I am only redirected.
Is there any way to do this work, Absolutely? Or do I work here on idiots?
Most of the data passing through one form from another form is not too dry, but I want to Do not know in a way that way.

Anyone Help here is appreciated!

Since coding_form is not an example of ModelForm and therefore coding_form.save () will not work and will not return a model instance You can access form data using the coding_form.cleaned_data dictionary

No comments:

Post a Comment