Django modelformset_factory does not save to database - python

The issue is that Django modelformset_factory does not save to database when inputs are inserted into the forms. It appears that only else statement is reached after forms are submitted for processing. Clearly the issue is with part in views.py where if 'name' in request.POST: then do sth. Advise how to solve it would be highly appreciated. Thank you.
views.py
from django.shortcuts import render
from .forms import modelformset_factory, AssumptionsForm
from .models import Assumptions
model_names = ['Form1', 'Form2']
def get_assumptions(request):
if request.method == 'POST' and 'name' in request.POST:
formset = modelformset_factory(
Assumptions, form=AssumptionsForm, extra=5)
if formset.is_valid():
print('valid form')
for form in formset:
if form.is_valid():
print('in for loop after valid form1')
assumptions = form.save(commit='False')
assumptions.Name = 'name'
assumptions.save()
else:
formset = modelformset_factory(
Assumptions, form=AssumptionsForm, extra=5)
print('reached else')
return render(request, 'assumptions.html', {'formset': formset, 'model_names': model_names})
models.py
from django.db import models
from django.forms import ModelForm
class Assumptions(models.Model):
Worst_Case = models.FloatField(null=True, blank=True, default=None)
Grey_Case = models.FloatField(null=True, blank=True, default=None)
Red_Case = models.FloatField(null=True, blank=True, default=None)
Blue_Case = models.FloatField(null=True, blank=True, default=None)
Green_Case = models.FloatField(null=True, blank=True, default=None)
Best_Case = models.FloatField(null=True, blank=True, default=None)
Name = models.TextField(null=True, blank=True, default=None)
forms.py
from django import forms
from django.forms import modelformset_factory, ModelForm
from .models import Assumptions
class AssumptionsForm(ModelForm):
class Meta:
model = Assumptions
fields = ['Worst_Case', 'Grey_Case', 'Red_Case', 'Blue_Case', 'Green_Case', 'Best_Case']
exclude = ['Name']
assumptions.html
<div class="form">
<form action="" method="post">
{% csrf_token %}
{{ formset.management_form }}
{{ formset.non_form_errors.as_ul }}
{% for name in model_names %}
<h1>{{name}}</h1>
<table id="formset" class="form">
{% for form in formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %}">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" name="{{name}}" value="save" />
{% endfor %}
</form>
</div>

name needs to be in your request.POST,
doublecheck your template and add an input tag with a name='name'
<input name='name' />

Related

Django, problem with the comment form below the post

I just can't figure it out for a very long time, I made a conclusion of comments to posts from the admin panel. But I just can't figure out how to make a comment form right under the post for users to comment. Thanks to all!
models.py
class Post(models.Model):
photo = models.ImageField(upload_to='media/photos/',null=True, blank=True)
name_barber = models.CharField(max_length=30)
description = models.TextField(blank=True, null=True)
def __str__(self):
return self.description[:10]
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
name = models.CharField(max_length=30)
body = models.TextField(null=True)
add_date = models.DateTimeField(auto_now_add=True)
enter code here
def __str__(self):
return '%s - %s' % (self.post, self.name)
form.py
class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ('name', 'body')
views.py
class HomePage(ListView):
model = Post
template_name = 'main/index.html'
context_object_name = 'posts1'
class BarbersPage(ListView):
model = Post
template_name = 'main/barbers.html'
context_object_name = 'posts'
urls.py
urlpatterns = [
path('',views.HomePage.as_view(),name='index'),
path('barbers/',views.BarbersPage.as_view(), name='barbers'),
]
barbers.html
{% for post in posts %}
<img src="{{MEDIA_URL}}{{post.photo.url}}" width="800" />
<h3>
{{ post.name_barber}}
</h3>
<p>{{ post.description}}</p>
<h3> Comments.. </h3>
{% if not post.comments.all %}
no comments yet...Add one
{% else %}
{% for comment in post.comments.all %}
<strong>
{{ comment.name }}
{{ comment.add_date }}
</strong>
<p>{{comment.body }}</p>
<br>
{% endfor %}
{% endif %}
{% endfor %}
You will need to add a form block below the post for users to add a comment.
Something like this:
{% for post in posts %}
<img src="{{MEDIA_URL}}{{post.photo.url}}" width="800" />
<h3>
{{ post.name_barber}}
</h3>
<p>{{ post.description}}</p>
<h3> Comments.. </h3>
{% if not post.comments.all %}
no comments yet...Add one
{% else %}
{% for comment in post.comments.all %}
<strong>
{{ comment.name }}
{{ comment.add_date }}
</strong>
<p>{{comment.body }}</p>
<br>
{% endfor %}
{% endif %}
Add Comment:<br/>
<form method="post" action="/AddComment">
<input type="hidden" id="postid" value="{{post.id}}"/>
<input type="text" id="newcomment" size="50"/><input type="submit" value="Submit"/>
</form><br/>
{% endfor %}
You will need to decide where to submit the comment data and update the action attribute in the form.

Django Formset.is_valid missing 1 required positional argument: 'self'

I was debugging my program and I noticed that on the line Formset.is_valid i am getting error:
missing 1 required positional argument: 'self'
Could you please advise how it could be solved to make formset and all the forms to validate successfully and save to database. Also, is double validation (formset and form) necessary? Thank you for insights.
views.py
from django.shortcuts import render
from .forms import modelformset_factory, AssumptionsForm
from .models import Assumptions
import pdb
model_names = ['Form1', 'Form2']
def get_assumptions(request):
if request.method == 'POST':
print('Reached post')
formset = modelformset_factory(
Assumptions, form=AssumptionsForm, extra=5)
pdb.set_trace()
for thing in model_names:
if thing in request.POST:
print('template name in model_names')
if formset.is_valid():
for form in formset:
if form.is_valid():
print('in for loop after valid form1')
assumptions = form.save(commit='False')
assumptions.Name = thing
assumptions.save()
else:
formset = modelformset_factory(
Assumptions, form=AssumptionsForm, extra=5)
print('reached else')
return render(request, 'assumptions.html', {'formset': formset, 'model_names': model_names})
models.py
from django.db import models
from django.forms import ModelForm
class Assumptions(models.Model):
Worst_Case = models.FloatField(null=True, blank=True, default=None)
Grey_Case = models.FloatField(null=True, blank=True, default=None)
Red_Case = models.FloatField(null=True, blank=True, default=None)
Blue_Case = models.FloatField(null=True, blank=True, default=None)
Green_Case = models.FloatField(null=True, blank=True, default=None)
Best_Case = models.FloatField(null=True, blank=True, default=None)
Name = models.TextField(null=True, blank=True, default=None)
assumptions.html
<div class="form">
<form action="" method="post">
{% csrf_token %}
{{ formset.management_form }}
{{ formset.non_form_errors.as_ul }}
{% for vardas in model_names %}
<h1>{{vardas}}</h1>
<table id="formset" class="form">
{% for form in formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %}">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="hidden" name="{{vardas}}" />
{% endfor %}
<input type="submit" value="Next">
</form>
</div>
forms.py
from django import forms
from django.forms import modelformset_factory, ModelForm
from .models import Assumptions
class AssumptionsForm(ModelForm):
class Meta:
model = Assumptions
fields = ['Worst_Case', 'Grey_Case', 'Red_Case', 'Blue_Case', 'Green_Case', 'Best_Case']
exclude = ['Name']
You haven't instantiated your formset. A factory is something that returns a class, so here modelformset_factory returns a formset class that you then need to instantiate in both your get and post blocks.
def get_assumptions(request):
AssumptionsFormset = modelformset_factory(
Assumptions, form=AssumptionsForm, extra=5)
if request.method == 'POST':
formset = AssumptionsFormset(request.POST)
if formset.is_valid():
...
else:
formset = AssumptionsFormset()
return render(request, 'assumptions.html', {'formset': formset, 'model_names': model_names})
Also note, you don't need to check is_valid() on each form; if the formset is valid, all the forms will be valid.

Django scaling and saving forms with inheritance

I am quite new to Django. I am currently using ModelForm to save user input data to model. However, I would like to save data to other child models which inherits from parent model. However, my code saves data to the parent model. I guess that save() function should be overwritten to save data to child model, but I have no idea how to do it. Also, how should I scale my template or forms.py to include more forms with different submit button names(or any other method so that submitted forms could be distinguished and database populated accordingly)? Any insights highly appreciated. Please find my code below.
forms.py
from django import forms
from django.forms import modelformset_factory, ModelForm
from .models import Assumptions, Scenario1_Assumptions,
Scenario2_Assumptions, Scenario3_Assumptions, Scenario4_Assumptions ...
Scenario_N_Assumptions
class AssumptionsForm(ModelForm):
class Meta:
model = Assumptions
fields = ['Worst_Case', 'Grey_Case', 'Red_Case', 'Blue_Case',
'Green_Case', 'Best_Case']
exclude = ()
AssumptionsFormSet = modelformset_factory(Assumptions, form=AssumptionsForm,
extra = 5)
views.py
from django.shortcuts import render
from .forms import AssumptionsFormSet, modelformset_factory
from .models import Scenario1_Assumptions, Assumptions
def get_assumptions(request):
if request.method == 'POST':
formset = AssumptionsFormSet(request.POST)
if formset.is_valid():
if 'scenario1' in request.POST:
for form in formset:
Scenario1_Assumptions = form.save() #THIS DOESN'T WORK
if 'scenario2' in request.POST:
for form in formset:
Scenario2_Assumptions = form.save() #and so on...
else:
formset = AssumptionsFormSet()
return render(request, 'assumptions.html', {'formset': formset})
models.py
from django.db import models
from django.forms import ModelForm
class Assumptions(models.Model):
Worst_Case = models.FloatField(null=True, blank=True, default=None)
Grey_Case = models.FloatField(null=True, blank=True, default=None)
Red_Case = models.FloatField(null=True, blank=True, default=None)
Blue_Case = models.FloatField(null=True, blank=True, default=None)
Green_Case = models.FloatField(null=True, blank=True, default=None)
Best_Case = models.FloatField(null=True, blank=True, default=None)
class Scenario1_Assumptions(Assumptions):
pass
class Scenario2_Assumptions(Assumptions):
pass
class Scenario3_Assumptions(Assumptions):
pass
class Scenario4_Assumptions(Assumptions):
pass
.
.
.
class Scenario_N_Assumptions(Assumptions):
pass
Assumptions.html
<form action="" method="post">
{% csrf_token %}
{{ formset.management_form }}
{{ formset.non_form_errors.as_ul }}
<table id="formset" class="form">
{% for form in formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %}">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit", name = "scenario1", value="Submit" />
</form>

Pass related object's field in Django ModelForm

I have a Django model class that extends the django.contrib.auth.models.User:
class MyModel(models.Model):
user = models.OneToOneField(User, models.CASCADE, unique=True)
bio = models.TextField()
date_of_birth = models.DateField()
and out of this model I'm making a ModelForm:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ['bio', 'date_of_birth']
How can I tell the MyModelForm to take in the User's fields such as username, first_name, last_name?
Example in the class Meta fields I can add ['bio', 'date_of_birth', 'user'] but that will just provide me with the dropdown to select to whom shall the MyModel in the MyModelForm be related to.
Doing ['bio', 'date_of_birth', 'user.first_name'] throws an exception django.core.exceptions.FieldError: Unknown field(s) (user.first_name) specified for MyModel
Edit: here's the traceback
https://pastebin.com/0T42VKEP
I kinda found my own solution, by overriding ModelForm's __init__ method:
class MyModelForm(forms.ModelForm):
first_name = forms.CharField()
class Meta:
model = MyModel
fields = ['bio', 'date_of_birth']
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
my_user = kwargs.get('instance')
first_name = my_user.user.first_name
self.fields['first_name'].initial = first_name #this will show the first name in the html page when i request the instance
And when I'm instantiating my form I just pass an instance of MyModel as a
kwarg: form = MyModelForm(instance=my_model_instance)
You can use InlinFormset. Here I am posting a code sample used by me that might help you. I have modified a bit according to your need, so if anything doesn't work let me know.
#forms.py
class UserForm(forms.ModelForm):
class Meta:
model = User
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
then in your views you should do like
def profileEdit(request,username):
# querying the User object with pk from url
user = User.objects.get(username=username)
# prepopulate MyModelForm with retrieved user values from above.
user_form = UserForm(instance=user)
MyModelInlineFormset = inlineformset_factory(User, MyModel,can_delete=False, fields="__all__")
formset = MyModelInlineFormset(instance=user)
if request.user.is_authenticated() and request.user.id == user.id:
if request.method == "POST":
user_form = UserForm(request.POST, request.FILES, instance=user)
formset = MyModelInlineFormset(request.POST, request.FILES, instance=user)
if user_form.is_valid():
created_user = user_form.save(commit=False)
formset = MyModelInlineFormset(request.POST, request.FILES, instance=created_user)
if formset.is_valid():
created_user.save()
formset.save()
return HttpResponseRedirect('/')
return render(request, "profile_edit.html", {
'title':'Edit -'+ user.get_full_name(),
"user_form": user_form,
"formset": formset,
})
else:
raise PermissionDenied
and in your profile_edit.html
{% extends "layout.html" %}
{% block content %}
{% if messages %}
<div>
{% for message in messages %}
<div class="alert alert-{{ message.tags }}"> <!-- singular -->
<a class="close" data-dismiss="alert">×</a>
{{ message|safe }}
</div>
{% endfor %}
</div>
{% endif %}
<div class="col-xs-8 col-xs-offset-2">
<div class="card">
<div class="card-content">
<h2 class="flow-text">Update your information</h2>
<form action="." method="POST" class="padding" enctype="multipart/form-data">
{% csrf_token %}
{% for field in user_form %}
<div class="form-group">
{{ field.errors }}
<label for="{{ field.label }}" >{{ field.label }}</label>
{{ field }}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text|safe }}</small>
{% endif %}
</div>
{% endfor %}
{{ formset.management_form }}
{% for form in formset %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.errors }}
<label for="{{ field.label }}" >{{ field.label }}</label>
{{ field }}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text|safe }}</small>
{% endif %}
</div>
{% endfor %}
{% endfor %}
<input type="submit" class="btn btn-primary" value="Save"></input>
</form>
</div>
</div>
</div>
{% endblock content %}

Is it possible to have two verbose_names in a Django Model?

I have a model that is defined like so...
class Message(models.Model):
email_subject = models.CharField(max_length=100, null=False, blank=False)
email = models.TextField(null=False, blank=False)
In want to be able to assign each of those fields two separate verbose_names(one in English, and one in Arabic). So that when I make a ModelForm, and render it to my template... I can do something like this...
{% for field in form %}
<p>
{% if field.field.required %}
<label for="{{ field.arabic_verbose_name}}" class='RequiredLabel'>{{ field.name }}</label>
{% else %}
<label for="{{ field.auto_id }}">{{ field.verbose_name}}</label>
{% endif %}
{% if field.errors %}
<div class='FieldErrorBox'>{{ field }}</div>
{% else %}
{{ field }}
{% endif %}
</p>
{% endfor %}
Is this possible?
from django.utils.translation import ugettext_lazy
class Message(models.Model):
email_subject = models.CharField(ugettext_lazy('Message field', 'email subject'), max_length=100, null=False, blank=False)
email = models.TextField(ugettext_lazy('Message field', 'email address'), null=False, blank=False)
Django internationalization can take care of this task. Coupled with a package like django-modeltranslation can often simplify the task. It's worth doing some tutorials to connect the dots. When using translatable strings in templates, there's tags (generally {% trans %}) to be used so that the correct lookups are performed.

Categories

Resources