Storing value in django models without using Model forms - django

I am having very complex models in my project, that I cannot display easily on HTML page using modelForms.
So I want to to use normal forms instead and store the values in Models.is it possible?

It is possible. You simply create your own form by inheriting forms.Form.
Then get the form input from request in your views and create your object.
In short what you are asking is possible.
Here is the official documentation for forms: https://docs.djangoproject.com/en/1.8/topics/forms/

Related

Do I need to create forms.py for my forms in Django?

I'm about to create a form in my website made in Django with elements that have complex input types from different plugins (like calendar plugins). Also, I'll be using a Validator plugin which validates the input before submission.
My question is do I need to create forms.py and like model the form or can I just create the form manually? The former seems like a very hassle process. Which one is more efficient and recommended?
As #dmitryro said you can create your forms manually in the templates and then getting in the request. It's recommended to use the forms api provided by Django since it allows you to reuse, validate, and customize your forms.
As to whether or not it is a good practice that depends completely on you but if you are trying to scale an application I would recommend use the forms.
It is good to use Django's built in form.
If we use django's form then we only have to write python code and django will create corresponding html for it. And our code will be short and clean.

Use cases for overriding save method in Django

Suppose I have a ModelForm and want to save the form. I know that we can override save method in django in the forms and models python code. But I want to know when exactly we should use saving forms in views, override save in forms and override save in models?
There's no right answer.
All you can do is choosing case by case:
Forms:
Specific methods will be used specifically when model is saved using this form.
Models:
Method will (almost) be used when saving models.
A third way you don't mention is Signals.

When to use form vs model validation?

Just curious. What is the best practice for when to use form vs model validation?
From what I understand currently, form validation should be used for:
AJAX / HTTP requests params
Forms that do not correlate to a model?
Another question is: I have a HTML form that roughly correlates to a model instance, do I use a ModelForm for it?
Definitely use ModelForm, if your form resembles model object even in a tiny bit.
If there are some minor differences (e.g. you don't use some of the fields or you want to use different error messages etc.) it's much easier to customize ModelForm then to use Form and implement all this functionality from scratch.
For more reference regarding ModelForm please checkout PyDanny's Core Concepts of Django ModelForms.
I am also trying to understand what is the difference/relation between form and model validation and I would like to share my notes that are formed after reading several docs.
I am currently interested in Creating Forms from Models
#mariodev shared the document Core Concepts of Django ModelForms and this provided a good start.
ModelForms select validators based off of Model field definitions
The main story behind the scenes seems to be the DRY principal. This article explains very well what exactly is the case here.
All right, all this is fair. The question is "Where in the Django Documentation is this explained"?
I bumped on a very brilliant article where it states that:
The form.full_clean() method is called by django at the start of the validation process (by form.is_valid(), usually immediately after the view receives the posted data).
Correct me if I am wrong but that line reads that everytime I enter data and hit 'enter' the validation process begins!
OK, this is simple now:
The validation on a ModelForm begins when we hit 'enter'.
Django first validates the form by checking one by one every applicable validation method on Fields, Field Subclasses (This is the documentation for a model's field subclass, not for a form field subclass), Form Subclasses and ModelForm (since it is a ModelForm).
Finally, it validates the Model Instance.
This is how all this works theoretically. The only thing that remains is to implement it.

Django forms lose related data

It appears that there's no way for me to get an ORM object with numerous select_related objects and create a Form and run my data through all the cleaning and additional validations I have.
I'm simply trying to reduce the number of queries that get executed, the form either takes an id and re-creates the object, or does not take advantage of the additional fields (I've tried using model_to_dict)
Is there anything I'm missing? How can I use select_related inside of a form?
Django 1.4
Do:
form = MyFormClass(instance=my_object)
Your form will need to be a ModelForm, see the docs for more information!

create form in templates(html) or in forms.py? :django

I'm new in django and python. I want to have a form in one of my website's page. When user fills the data, i want to create an xml file with user input. so i need to access the user's data (form data) from views.py. I want to know am i allowed to only have form in html? if yes, how can i use it's data in views.py? i mean should i have always a class defined in forms.py that contains my form fields? If the second way is a rule, what should i do with templates(html)? (It means if i define my fields in a class in forms.py, should i redefine them in templates(html) too? and how can i use data in views.py)
i need some kinds of input in my form: checkbox, input type:text and input type:file
I really need your answers. Thank you :)
You can totally define your form in raw html and write a view which just processes the post data. But then we come to point where you would invent the wheel once again. Django provides a bunch of field classes which provide html rendering and data validation (never trust user input).
I suggest you read the online documentation for forms.