Django form with inputs populated from database - django

I have a simple Django model structure with Products and Orders, linked by an intermediary table that determines the quantity of each product within a given order:
models.py:
class Product(models.Model):
name = models.CharField()
class Order(models.Model):
id = models.IntegerField()
products = models.ManyToManyField(Product, through='OrderProduct')
class OrderProduct(models.Model):
order=models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveSmallIntegerField()
I'd like to design a form in my forms.py file that 1) pulls a list of every product in the database; 2) gives the user the option to adjust the quantity of each product ordered (e.g., in a CharField input), and 3) creates new Order and OrderProduct objects that store a record of the quantity of each Product ordered by the user.
However, I can't figure out a good way to create a form that will automatically pull all Products from the database and add fields to represent the quantity ordered of the given Product. Ideally the result would look something like this, but coded automatically:
forms.py:
class OrderForm(forms.Form):
product_1_quantity = forms.IntegerField()
product_2_quantity = forms.IntegerField()
product_3_quantity = forms.IntegerField()
....etc., for each product. What's the best way to do this? Does it involve a 'for' loop that creates a new field for each product found in the database?

I think Django Formsets might be useful for you. You can create sets of the same form over and over depending based on number you decide, hence your products. Check it out here . You can do stuff such as in the below example.
class ArticleForm(forms.Form):
title = forms.CharField()
from django.forms import formset_factory
ArticleFormSet = formset_factory(ArticleForm, extra=2)
You can also pre-populate it with data such as,
formset = ArticleFormSet(initial=[
{'title': 'Django is now open source'}
])
Hope this helps!

Elaborating on the answer below from user3140312 — Django's formsets provided the solution!
models.py:
class Product(models.Model):
name = models.CharField()
class Order(models.Model):
id = models.IntegerField()
products = models.ManyToManyField(Product, through='OrderProduct')
class OrderProduct(models.Model):
order=models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveSmallIntegerField()
forms.py:
class ProductForm(forms.Form):
product_id = forms.IntegerField(widget=forms.HiddenInput)
quantity = forms.IntegerField()
views.py:
from .forms import ProductForm
from django.forms import formset_factory
from .models import Product
# Show a form asking for the user's product quantities
def product_form(request):
# If this form is being processed...
if request.method == 'POST':
ProductFormSet = formset_factory(ProductForm)
# Grab the form from the POST variable
formset = ProductFormSet(request.POST)
# If the form is valid...
if formset.is_valid():
# Do whatever you want with the results
# If the form is not being processed, create a new ProductFormSet and render it
else:
product_list = []
for product in Product.objects.all():
product_list.append({'product_id': product.id, 'quantity': 0})
ProductFormSet = formset_factory(ProductForm, extra=0)
formset = ProductFormSet(initial=product_list)
return render(request, 'template.html', {'formset': formset})
template.html:
<form action="{% url '' %}" method="post">
{% csrf_token %}
{{ formset.as_table }}
{{ formset.management_form }}
<input type="submit" value="Submit" />
</form>

You can use ModelChoiceField to show all the products without do a for loop:
class OrderForm(forms.Form):
product = forms.ModelChoiceField(queryset=Product.objects.all()) # with queryset you can show a checkbox with the query
quantify = forms.IntegerField()
Now your views should take care of save your data. If for example you need a button "Add new" you can use ajax otherwise you can use formset

Related

How to store user auto in database?

I created a form for adding products to an e-Commerce site. The form isn't working perfectly.
First issue: I want to store the user automatically by submitting the form. I actually want to store Who did add the product individually.
Second Issues: The image field is not working, the image is not stored in the database.
How can I fix these issues? help me
forms.py:
from django import forms
from .models import Products
from django.forms import ModelForm
class add_product_info(forms.ModelForm):
class Meta:
model = Products
fields = ('product_title','product_image')
model.py:
class Products(models.Model):
user = models.ForeignKey(User, related_name="merchandise_product_related_name", on_delete=models.CASCADE, blank=True, null=True)
product_title = models.CharField(blank=True, null=True, max_length = 250)
product_image = models.ImageField(blank=True, null=True, upload_to = "1_products_img")
views.py:
def add_product(request):
if request.method == "POST":
form = add_product_info(request.POST)
if form.is_valid():
form.save()
messages.success(request,"Successfully product added.")
return redirect("add_product")
form = add_product_info
context = {
"form":form
}
return render(request, "add_product.html", context)
templates:
<form action="" method="POST" class="needs-validation" style="font-size: 13px;" novalidate="" autocomplete="off" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<div class="d-flex align-items-center">
<button type="submit" class="btn btn-outline-dark ms-auto" style="font-size:13px;">Add</button>
</div>
</form>
You need to set the .user of the .instance wrapped in the form to the logged in user (request.user). Furthermore you need to pass both request.POST and request.FILES to the form to handle files.
from django.contrib.auth.decorators import login_required
#login_required
def add_product(request):
if request.method == 'POST':
form = add_product_info(request.POST, request.FILES)
if form.is_valid():
form.instance.user = request.user
form.save()
messages.success(request, 'Successfully product added.')
return redirect('add_product')
else:
form = add_product_info()
context = {
'form': form
}
return render(request, 'add_product.html', context)
I would also advise not to use null=True nor blank=True, unless a field is really optional. Likely the product_title should not be optional, nor should the user be, since you use CASCADE in case the user is removed.
Note: You can limit views to a view to authenticated users with the
#login_required decorator [Django-doc].
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
Note: Forms in Django are written in PascalCase, not snake_case,
so you might want to rename the model from add_product_info to ProductInfoForm.
Note: normally a Django model is given a singular name, so Product instead of Products.
why are you using the ForeignKey with your user. the first issue i notice is with the class Meta. Pass this as a list not tuple.
class add_product_info(forms.ModelForm):
class Meta:
model = Products
fields = [
'product_title',
'product_image',
]
then try this as well.
class Products(models.Model):
user = models.OneToOneField(User, related_name="merchandise_product_related_name", on_delete=models.CASCADE, blank=True, null=True)

Django get an instance of a object and pass it through to a form

I am new to Django and programming in general. I am trying to generate a list of records from a database but with two fields that can be edited.
In the browser it should show a line with the fields:
clientcode, clientname, Reason, comment
Name and description come from the model and are a reference. The user should only be able to capture reason and comments
I have created a forms.py file and a ModelForm. My issue is how do I pass through an individual object. For this example I've limited my dataset to 10 records
In my view file
def home(request):
if request.method == 'GET':
nca = NcaRe.objects.all()[:10]
form = NcaReForm(instance= <what should go in here> )
return render(request, 'NCAComments/home.html', {'form': form, 'nca': nca})
else:
pass
In my model I have a field called primarykey. I'm not sure how to pass this to the form so that I only bring in that record. I have tried looking at the documentation but have not been able to follow it.
My Model py.
from django.db import models
class NcaRe(models.Model):
primarykey = models.IntegerField(blank=True, null=False, primary_key=True)
clientcode = models.CharField(db_column='ClientCode', max_length=200, blank=True, null=True)
clientname = models.CharField(db_column='ClientName', max_length=510, blank=True, null=True)
reason = models.TextField(blank=True, null=True)
comment = models.TextField(blank=True, null=True)
class Meta:
db_table = 'NCA_RE'
Forms.py
from django.forms import ModelForm
from .models import NcaRe
class NcaReForm(ModelForm):
class Meta:
model = NcaRe
fields = ['reason', 'comment']
In html I am trying to loop through and pass the form
{% for n in nca %}
<p> {{n.clientcode}}</p>
<form>
{% csrf_token %}
{{ form }}
</form>
{% endfor %}
In general, you need to just return empty form if the method of request if GET like as form(). I write below sample code that you can do your calculation in after form validation form.is_valid()
views.py
from django.shortcuts import render
from testPhilip.forms import NcaReForm
from testPhilip.models import NcaRe
def home(request):
if request.method == 'GET':
nca = NcaRe.objects.all()[:10]
form = NcaReForm()
elif request.method == 'POST':
form = NcaReForm(request.POST)
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return render(request, 'testPhilip/home.html', {'form': form, 'nca': nca})
You can retrieve the data after form validation in a cleaned format like this:
comment = form.cleaned_data['comment']
Update:
If you want to populate your form fields with values from database or any default values, you can pass them in the 'GET' section as below:
nca_object=NcaRe.objects.get(pk=nca_id)
form=NcaReForm({
'comment':nca_object.comment,
'reason':nca_object.reason,
})
For more information about writing forms refer to Django forms doc

Can not add values from forms to Django models

I made a form and there I had a multiple-choice field called artists which I got from my database and while adding a song a user can select multiple artists and save the song.
The artists are a ManyToManyField in Django models.
models.py
class Artists(models.Model):
""" Fields for storing Artists Data """
artist_name = models.CharField(max_length = 50, blank = False)
dob = models.DateField()
bio = models.TextField(max_length = 150)
def __str__(self):
return self.artist_name
class Songs(models.Model):
""" Fields for storing song data """
song_name = models.CharField(max_length = 30, blank = False)
genre = models.CharField(max_length = 30, blank = False)
artist = models.ManyToManyField(Artists)
release_date = models.DateField()
forms.py
class Song_input(forms.Form):
queryset = Artists.objects.only('artist_name')
OPTIONS = []
for i in queryset:
s = []
s = [i, i]
OPTIONS.append(s)
artist_name = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,choices=OPTIONS)
song_name = forms.CharField()
genre = forms.CharField()
release_date = forms.DateField(widget=DateInput)
Now I want to get all the values selected from the form and save to my database. Here the artist_name may have multiple values.
I have tried using the add() and create() methods but can not figure out how to add all the data where one field (artist_name) having multiple data to my database.
I strongly advise to make use of a ModelForm [Django-doc]. Especially since you make use of ManyToManyFields, which are more cumbersome to save yourself.
# app/forms.py
from django import forms
class SongForm(forms.ModelForm):
class Meta:
model = Songs
fields = '__all__'
widgets = {
'artist': forms.CheckboxSelectMultiple,
'release_date': forms.DateInput
}
There is thus no need to specify the fields yourself, you can change the widgets by adding these to the widgets dictionary [Django-doc] of the Meta subclass.
In your view, you can then both render and sae objects with that form:
# app/views.py
from app.forms import SongForm
def add_song(request):
if request.method == 'POST':
form = SongForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('name-of-some-view')
else:
form = SongForm()
return render(request, 'some-template.html', {'form': form})
The form.save() will save the object in the database.
In the template, you can then render the template:
<form method="post" action="{% url 'name-of-add_song-view' %}">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>

Django 2.x - using ModelForm to create a list in a form with unlimited items

I am attempting to create a ModelForm that contains (but is not just) a field where unlimited items can be added. To clarify, the common ToDo list example is almost exactly what I want to do, where users can add an item in an input and it is displayed to the user as added to the ToDo list. In it, the user can add as many items to a list as they want before submitting the form that also contains other fields like a title and description.
I have seen how to do this with custom forms, but I am trying to master ModelForms and keep all the forms on this learning project as ModelForms.
My question is, is this possible with ModelForms? I have thought that perhaps an array can be created, where each added item can be appended to the array, and on the final form submission that array can be stored in the db, but it is above my skill level to actually implement that. Is this the right track, or is there a better way? Thanks for helping me learn!
You don't need to store arrays in databases. Just create a new table for the arrays items. In this case, you have to create a Django model for "ToDo" with a many-to-one relationship with User (or List). For example:
class User(models.Model):
name = models.CharField(max_length=30, primary_key=True, blank=False)
# etc...
class ToDo(models.Model):
title = models.CharField(max_length=50, primary_key=True, blank=False)
description = models.TextField(blank=True, null=True)
user = models.ForeignKey(User, blank=True on_delete=models.CASCADE)
Then create its forms:
class UserForm(forms.ModelForm):
class Meta:
fields = [...]
model = User
class ToDoForm(forms.ModelForm):
class Meta:
fields = [...]
model = ToDo
View:
if request.method == 'POST':
user_form = UserForm(request.POST)
todo_form = ToDoForm(request.POST)
if user_form.is_valid() and todo_form.is_valid():
user = user_form.save()
todo_form.save()
todo_form.cleaned_data['user'] = user
return HttpResponseRedirect('/success')
if request.method == 'GET':
context = {
'user_form': user_form,
'todo_form': todo_form,
}
else:
context = {
'user_form': UserForm(),
'todo_form': ToDoForm(),
}
return TemplateResponse(request, 'your_template.html', context)
Template:
<form action="your_view" method="POST">
{% csrf_token %}
{{ user_form.as_p }}
{{ todo_form.as_p }}
<button type="submit">
</form>

Django form with fields from two different models

I need to display one form, with multiple fields from 2 different models.
Form will contain only part of fields from models, and layout will be made using the crispy forms.
My models:
class Company(BaseModel):
title = models.CharField(_('Company'), max_length=128)
domain = models.CharField(_('Domain'), max_length=128)
class Account(BaseModel):
company = models.ForeignKey(Company)
user = models.OneToOneField(User)
role = models.CharField(_('Role'), choices=ROLES, default='member', max_length=32)
Fields which I want to show in form:
company title, user first name, user last name, user email
Is it even possible? How can I do this?
The other answers on this page involve tossing away the benefits of model forms and possibly needing to duplicate some of the functionality you get for free.
The real key is to remember that one html form != one django form. You can have multiple forms wrapped in a single html form tag.
So you can just create two model forms and render them both in your template. Django will handle working out which POST parameters belong to each unless some field names clash - in which case give each form a unique prefix when you instantiate it.
Forms:
class CompanyForm(forms.ModelForm):
class Meta:
fields = [...]
model = Company
class AccountForm(forms.ModelForm):
class Meta:
fields = [...]
model = Account
View:
if request.method == 'POST':
company_form = CompanyForm(request.POST)
account_form = AccountForm(request.POST)
if company_form.is_valid() and account_form.is_valid():
company_form.save()
account_form.save()
return HttpResponseRedirect('/success')
else:
context = {
'company_form': company_form,
'account_form': account_form,
}
else:
context = {
'company_form': CompanyForm(),
'account_form': AccountForm(),
}
return TemplateResponse(request, 'your_template.html', context)
Template:
<form action="." method="POST">
{% csrf_token %}
{{ company_form.as_p }}
{{ account_form.as_p }}
<button type="submit">
</form>
In your forms.py
from django import forms
class YourForm(forms.Form):
title = forms.CharField()
first_name = forms.CharField()
last_name = ...
In your views.py
from forms import YourForm
from django import views
from models import Company, Account
class YourFormView(views.FormView)
template_name = 'some_template.html'
form_class = YourForm
success_url = '/thanks/'
def form_valid(self, form):
title = form.cleaned_data['title']
...
# do your processing here using Company and Account
# i.e. company = Company.objects.create(title=title, ...)
# account = Account.objects.get_or_create(
# user=..., company=company ...)
# ... more processing
#
# Call company.save() and account.save() after adding
# your processed details to the relevant instances
# and return a HttpResponseRedirect(self.success_url)
def is_valid(self):
# don't forget to validate your fields if need be here
As usual the docs are pretty helpful.
https://docs.djangoproject.com/en/1.7/topics/forms/