1

I would like to display choices of Test_Baustoff->art Model in my Test_Objekt Model Form.

Right now i am trying to solve it with a Widget...

Models:

class Test_Objekt(models.Model):
   baustoffid = models.ForeignKey(Test_Baustoff, on_delete=models.CASCADE)
   bezeichnung = models.CharField(max_length=100, default='', blank=True)

class Test_Baustoff(models.Model):
   art = models.CharField(max_length=100)
   wert = models.IntegerField(default='0')

Forms: (found this code in the django docs... don't know if i am using it in the right way??)

class BaustoffidSelect(forms.Select):
    def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
        option = super().create_option(name, value, label, selected, index, subindex, attrs)
        if value:
            option['attrs']['data-art'] = value.instance.art
        return option

class ObjektForm(forms.ModelForm):

        class Meta:
        model = Test_Objekt
        fields = ['bezeichnung', 'baustoffid', 'bauweiseid', 'dickeaussenwand', 'dickedaemmung', 'fensterqualitaet']
        labels = {'bezeichnung': 'Objekt-Bez'}
        widgets = {'baustoffid': BaustoffidSelect}

html template:

    <table class="table table-bordered table-light">
        {{objekt_form.as_table}}
    </table>

For the moment, I don't find a way to solve my problem. I looked some tutorials or StackOverflow questions but nothing up to now.

Do you have any idea about this handling?

3 Answers3

1

Try to add a list of tuples with model object instances:

Baustoff_choices = []
for i in Test_Baustoff.objects.all():
    Baustoff_choices.append(
        (i.id,i.art)
    ) #second element is what is this what will be displayed in template

Then in your forms.ModelForm:

baustoffid = forms.ChoiceField(choices=Baustoff_choices)
mka
  • 153
  • 5
0

Oh Thanks a lot. This works fine with the id!

I was already trying this approach but made the mistake that i wanted to implement this into my models... I did not know that i can also add a extra field to my forms.modelform

what i was asking myself. is it also possible to not save the id but a foreykn key object

like in models:

  #baustoffid = models.ForeignKey(Test_Baustoff, on_delete=models.CASCADE)

and in forms:

for i in Test_Baustoff.objects.all():
    Baustoff_choices.append(
        (**i**, i.art) # or somithing like **i.instance**
    ) 

but i get: Cannot assign "'Test_Bauweise object (2)'": "Test_Objekt.bauweiseid" must be a "Test_Bauweise" instance.

kind regards!

  • You want to create a new assigned `Test_Objekt` when someone choose one of your `Test_Baustoff`? – mka Jan 27 '21 at 19:29
  • yes kind of. when i use a foreignkey field in my forms, i can choose from the foreign key objects, but the user only gets shown **-Object(1) -Object(2)** in the html file. what i want to do is, that the user can choose from `Test_Baustoff.art`, and after the user hast chosen i want to work with the Object corresponding to the `.art` field. you get me ? – Bernhard Developing Jan 29 '21 at 08:40
  • solved. missed to add this function in my models.py so it got displeyed like "Object(1)"... def __str__(self): return self.art – Bernhard Developing Jan 29 '21 at 09:08
0

solved. missed to add this function in my models.py so it got displeyed like "Object(1)"...

def __str__(self): return self.art