Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django forms: How to simply include all attributes in the associated model

Tags:

I am working through how to use Django's forms (https://docs.djangoproject.com/en/1.11/topics/forms/#more-on-fields) and I can't see a way to generate a form structure that is based on a defined Model. In Symfony, I remember I was able to get my form to automatically include all parameters of myModel (for example) even if any new attributes were later added to the model.

For example:

class myModel(models.Model):
    name = models.CharField(max_length=50)
    created=models.DateTimeField(null=False)
    modified=models.DateTimeField(null=True)
    myParameter= models.IntegerField(default=None)
    // ... plus many more parameters

Rather than having to manually type corresponding rows into my class myModelForm(forms.Form):, I'm looking/hoping for a 'catch all'.

like image 420
Bendy Avatar asked Aug 10 '17 06:08

Bendy


People also ask

What is form Is_valid () in django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

What is ModelForm in django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.

How can I get form data in django?

Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.


1 Answers

from django.forms import ModelForm

class myModelForm(ModelForm):
    class Meta:
        model = myModel
        fields = '__all__'

More details selecting-the-fields-to-use

like image 82
Brown Bear Avatar answered Oct 04 '22 16:10

Brown Bear