0

I need to generate Django forms.Form object with fields not from Model.fields (Database Table Columns names), but by records in Model.Table.

I have table Model in models.py:

class MntClasses(models.Model):
    type = models.CharField(max_length=2, blank=True, null=True)
    class_subtype = models.CharField(max_length=45, blank=True, null=True)
    text = models.CharField(max_length=45, blank=True, null=True)
    explanation = models.CharField(max_length=45, blank=True, null=True)
    name = models.CharField(max_length=45, blank=True, null=True)

views.py

# Form generate
class Form_classes(forms.Form):

    def __int__(self, *args, **kwargs,):
        super(Form_classes, self).__init__(*args, **kwargs)

        print("some")

        for fld_ in args:
            self.fields[fld_.name] = forms.BooleanField(label=fld_.text)

#Main
def page_Category_Main(request, post):

    db_table = MntClasses

    form_fld = db_table.objects.all()
'''
This QuerySet 20 records returned of <MntClasses: MntClasses object (0-19)> type.
QuerySet Filds Names: 'name','type','expalnation', 'text'
''':

    form_ = Form_classes(*form_fld)

    exit_ = {
        'form': form_,
    }

    return render(request, template_name="category.html", context=exit_)

It raise TypeError init() takes from 1 to 12 positional arguments but 20 were given

So, i have no idea what does it mean this code taken from were: Auto-generate form fields for a Form in django:

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

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

What is this "*args", how to use it?

How can I generate Form.fields by QuerySet form_fld.name in that case?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
shulya403
  • 422
  • 1
  • 6
  • 17

1 Answers1

0

About args

To understand what args is you can take a look at this post which will eventually direct you to https://docs.python.org/3/tutorial/controlflow.html#arbitrary-argument-lists.

Basically it's a special python syntax which allows a function to retrieve multiple arguments as a tuple in a single variable.

About Django's Models

Do you really want to generate multiple forms?

If this is the case you would need to loop over your table:

forms = []
db_table = MntClasses
for item in db_table.objects.all():
    forms.append(Form_classes(item))

exit_ = {
    'form': forms,
}

the trick is then how you deal with the multiple forms on the front end.

Also you probably want to switch to a ModelForm which would look something like:

from django import forms
from myapp.models import MntClasses

class FormMnt(forms.ModelForm):
    class Meta:
        model = MntClasses
    ...

In case you want to handle multiple instances of MntClasses in a single form, you should look at Django's formsets.

romaingz
  • 421
  • 2
  • 6
  • Thanks, but it seems no thing that i want. I need the form, that contain 20 checkboxes for any row of db_table = MntClasses (that have 20 records). I want html like:
    – shulya403 Aug 18 '20 at 21:06
  • A Django Form is used for data validation. Is it possible that what you want is a ListView? https://docs.djangoproject.com/en/3.1/ref/class-based-views/generic-display/#listview – romaingz Aug 19 '20 at 06:22