2017-08-28 4 views
0

Ich versuche, ein Modell Formset mit einem ModelChoiceField zu erstellen, so dass der Benutzer mehrere Formulare erstellen und in jedem Formular ein Element aus dem Abfrage-Set auswählen können. Wenn ich meine Formulargruppe rendere, erstellt es jedoch ein Formular für jedes Objekt in dem Abfrage-Set. In jedem Formular ist das ModelChoiceField bereits auf das nächste Objekt in dem Abfrage-Set festgelegt. Dies ist nicht wünschenswert, ich möchte nur ein Formular, wo der Benutzer das gewünschte Objekt auswählen sollte und ich werde sie dynamisch mit JS erstellen. Grundsätzlich verschachtele ich das Anwendungsformular innerhalb des Regelanforderungsformulars.Django ModelChoiceField Modell Formularsatz Formular für jedes Objekt erstellen

Forms:

class ApplicationForm(BootstrapMixin, forms.ModelForm): 

    name = forms.ModelChoiceField(
     queryset=Application.objects.all(), 
     to_field_name='name', 
     help_text='Application name', 
     error_messages={ 
      'invalid_choice': 'Application not found.', 
     } 
    ) 

    class Meta: 
     model = Application 
     fields = [ 
      'name' 
     ] 
     help_texts = { 
      'name': "Application name", 
     } 


class RuleRequestForm(BootstrapMixin, forms.ModelForm): 

    class Meta: 
     model = RuleRequest 
     fields = [ 
      'name', 'description', 
     ] 
     help_texts = { 
      'name': 'Short name for the request', 
      'description': "Buisness case and summary.", 
     } 

    def __init__(self, *args, **kwargs): 

     super(RuleRequestForm, self).__init__(*args, **kwargs) 

     data = kwargs.get('data') 
     initial = kwargs.get('initial') 

     application_formset = modelformset_factory(Application, form=ApplicationForm, extra=0, min_num=1) 

     self.applications = application_formset(data=data, initial=initial, prefix='applications') 

Modelle:

class Application(CreatedUpdatedModel): 

    name = models.CharField(
     max_length=255, 
     null=True 
    ) 
    description = models.TextField(
     blank=True 
    ) 

    def __str__(self): 
     return self.name 


class RuleRequest(CreatedUpdatedModel, ST2ExecuationModel): 

    name = models.CharField(
     max_length=255 
    ) 
    description = models.TextField(
     blank=False 
    ) 
    applications = models.ManyToManyField(
     to=Application, 
     related_name='rule_requests' 
    ) 
+0

so funktioniert das Formset. Sie müssen die gewünschte Funktionalität erstellen. –

+0

@ alfonso.kim Wie würde ich das tun? Ich möchte nur ein Formular initialisieren und das 'ModelChoiceField' sollte nichts ausgewählt haben. – lampwins

+0

posten Sie bitte Ihren Modellcode. –

Antwort

0

Sie mit einem einfachen Ansatz beginnen:

Ihrer Ansicht:

applications = Application.objects.all() 
return render('template_name.html', request, applications) 

in Ihrer Vorlage:

Sobald Sie das Verhalten für die Formulare definiert haben, können Sie den Code mit Djangos ModelForm reduzieren.

Bitte beachten Sie, dass dies keine vollständige Lösung für Ihr Problem ist. Es hängt davon ab, wie Sie die Formulare erstellen möchten.

Verwandte Themen