I have an form which allows a user to edit an object description.
How can I populate an object ID in a form's hidden input value.
What I done so far is I added an field called hidden_field in forms.py but it only show the hidden_field . How can I link the hidden_field with the object ID
models.py
class School(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=55)
description = models.CharField(max_length=300,blank=True)
forms.py
class SchoolDescriptionForm(forms.ModelForm):
description = forms.CharField(widget=forms.Textarea,max_length=300)
hidden_field = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = School
fields = ()
views.py
def SchoolEditor(request,school_id):
school = School.objects.get(pk=school_id,user=request.user)
form = SchoolDescriptionForm(instance=school) # I want to populate the object ID
return render(request,'schooleditor.html',{'school':school,'form':form})
template
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type = "submit" value= "save" />
{{ form.field.as_hidden }}
</form>
Change hidden_field to id and tell Django to include the School's id.
class SchoolDescriptionForm(forms.ModelForm):
description = forms.CharField(widget=forms.Textarea,max_length=300)
id = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = School
fields = ('id', 'name', 'description')
EDIT:
If you want to conserve hidden_field as name you should then add a custom init method:
def __init__(self, *args, **kwargs):
super(SchoolDescriptionForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['hidden_field'].initial = instance.id
Just pass the object id in the form initial:
def SchoolEditor(request,school_id):
initial = {}
school = School.objects.get(pk=school_id,user=request.user)
if school:
initial.update({'hidden_field': school.id})
form = SchoolDescriptionForm(instance=school, initial=initial) # I want to populate the object ID
return render(request,'schooleditor.html',{'school':school,'form':form})
Related
I have a django filter with a dependent drop down to filter car manufactures and models. The models use a charfield and pulls the cars from a db entry.
I would like a place holder to say manufacture and model on their respected fields.
I cant find much online about doing this. The only post I can find relates to using the choice field on the model which wont work for me.
filter
class CarFilterForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['model'].queryset = Post.objects.none()
if 'model_manufacture_id' in self.data:
try:
model_manufacture_id = int(self.data.get('model_manufacture_id'))
self.fields['model_id'].queryset = CarModels.objects.filter(model_manufacture_id=model_manufacture_id)
except (ValueError, TypeError):
pass
class carFilter(django_filters.FilterSet):
class Meta:
model = Post
fields = 'manufacture', 'model'
form = CarFilterForm
html
<form method="get" id="AllCarsForm" data-cars-url="{% url 'ajax-allcars' %}">
{% render_field myFilter.form.manufacture class="cars-filter-widget" %}
{% render_field myFilter.form.model class="cars-filter-widget" %}
<button class="btn btn-primary" type="submit">Search</button>
</form>
models
class Manufactures(models.Model):
manufacture_id = models.AutoField(primary_key=True)
manufacture = models.CharField(max_length=55, default="OEM")
class CarModels(models.Model):
model_id = models.AutoField(primary_key=True)
model = models.CharField(max_length=55)
model_manufacture = models.ForeignKey(Manufactures, on_delete=models.CASCADE)
Try to set the empty_label for the fields:
self.fields['your_field'].empty_label = 'My custom empty label'
The simplest method of doing this is to set the model field default to one that corresponds to your fields.
Example:
class Model(models.Model):
field = models.CharField(max_length=25, choices=CHOICES,
default=DEFAULT, blank=True)
You can also do this in forms:
self.fields['field'].choices = [('', 'Placeholder Text')] + list(
self.fields['field'].choices[1:])
Im trying to add a field called, interested_fields inside my personalInfo model which users can choose from and the choices themselves come from another models' objects with the help of ManyToMany relation between the two models. Here are my models.py codes(I simplified my personal model by removing some other fields like name, age, etc in order to make it more readable for you):
class Field(models.Model):
id = models.AutoField(primary_key=True)
slug = models.CharField(max_length=16, default='default')
title = CharField(max_length=32)
class PersonalInfo(models.Model):
id = models.AutoField(primary_key=True)
interested_fields = models.ManyToManyField(Field, blank=True)
then, I created a ModelForm like this:
class InterestedFieldsForm(forms.ModelForm):
interested_fields = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=Field.objects.all(), required=False)
class Meta:
model = PersonalInfo
fields = ['interested_fields']
and created a get and post functions inside my views like this:
class PersonalView(View):
template_name = 'reg/personal.html'
def get(self, request, *args, **kwargs):
context = {}
context['fields'] = Field.objects.all()
return render(request, self.template_name, context=context)
def post(self, request, *args, **kwargs):
user = request.user
if request.method == 'POST':
form = InterestedFieldsForm(request.POST)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
else:
form = InterestedFieldsForm()
return render(request, 'reg/done.html', context={'form': form})
and finally in template, inside the form I added this for loop:
{% for field in fields %}
<label class="containerq ant-col ant-col-md-6 ant-col-xs-8" >
<span>
<input type="checkbox" name="interested_fields" {% if field.slug in user.personalInfo.interested_fields %} checked="checked" {% endif %} value="{{field.title}}">
<span style="margin-left:7px" class="checkmark"></span>
</span>
<span>{{field.title}}</span>
</label>
{% endfor %}
when I submit the form it gives me this error:
cannot unpack non-iterable Field object
Im new to django so I really dont know what am I doing wrong. thank you for your answers
You should use a ModelMultipleChoiceField
interested_fields = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple, queryset=Field.objects.all(), required=False).
Forgive the logic of the table structure below example. It only meant as a simple example of the situation that I am.
So the situation is that I want to make an employee form page, where the department and line manager might or might not exist already. So I replace the drop-down box with the form field for the foreign key, so they can be created if need all in one step for the user. However, with this kind of dependency, I am not doing the right thing in view to make it work.
If you need more detail please do ask.
If you can make the Title more precise please do.
Thank you for your time.
Model
class Employee(models.Model):
name = models.CharField()
age = models.CharField()
line_manager = models.ForeignKey(LineManager)
department = models.ForeignKey(Department)
class LineManager(models.Model):
manager_name = models.CharField()
department = models.ForeignKey(Department)
class Department(models.Model):
department_name = models.CharField()
Form
class EmployeeForm(ModelForm):
class Meta:
model = Employee
fields = ['name',
'age'
#'line_manager' Not needed
#'department' Not needed]
exclude = ('line_manager', 'department')
class LineManagerForm(ModelForm):
class Meta:
model = LineManager
fields = ['manager_name']
exclude = ('department')
# There is a basic form for Department, as well.
View
class AddEmployeeView(View):
forms = {'department': DepartmentForm(self.request.POST or None),
'line_manager': LineManagerForm(self.request.POST or None),
'employee': EmployeeForm(self.request.POST or None)]}
def get(self, request, *args, **kwargs):
form_list = [form for _,form in forms]
return render (request, 'app/temp.html', {'forms': form_list}
def post(self, request, *args, **kwargs):
if all([form.is_valid() for _,form in forms]):
department_data = forms['department'].cleaned_data
department_obj, _ = Department.objects.get_or_create(department_data)
line_manager_instance = forms['line_manager'].instance
line_manager_instance.department = department_obj
line_manager_data = forms['line_manager'].cleaned_data
line_manager_obj, _ = LineManager.objects.get_or_create(line_manager_data)
employee_instance = forms['employee'].save(commit=False)
employee_instance.department = department_obj
employee_instance.line_manager = line_manager_obj
employee_instance.save()
html
<form method="POST">
{% csrf_token %}
{% form in forms %}
{{ form.as_p }}
{% endform %}
<input type='submit'/>
</form>
I have a problem with my app.
A pet store app.
I created 2 forms . The First form allow users to create their own store and save the data into the models which I did successfully and the second form allows users to add their own pets to the pet store.
The first form was successful because it got validated correctly but my second form didn't succeed the validation because in PetForm , I have a field called image = forms.FileField() where users can upload a picture of their pet and the validation fail because the picture does not save anywhere.
I tried to put in an argument into image = forms.FileField(upload_to="images/")
but I received an error
__init__() got an unexpected keyword argument 'upload_to'
I'm reading the documentation at the moment and it state ' When you I use FileField in a form, you must also remember to bind the file data to the form.'.
I'm having trouble understanding binding the file data .
Can someone please help me !
My forms.py
from django import forms
from pet.models import Store
from pet.models import Pet
class StoreForm(forms.ModelForm):
name = forms.CharField(max_length =20,widget =forms.Textarea)
number = forms.CharField(max_length =20,widget =forms.Textarea)
address = forms.CharField(max_length = 20,widget = forms.Textarea)
class Meta:
model = Store
fields = ('name','number','address')
class PetForm(forms.ModelForm):
animal =forms.CharField(max_length = 20,widget = forms.Textarea)
description =forms.CharField(max_length =20, widget = forms.Textarea)
owner = forms.ModelChoiceField(queryset=Store.objects.all())
image = forms.FileField()
class Meta:
model = Pet
fields = ('animal','description','owner','image')
My models.py
from django.db import models
class Store(models.Model):
name = models.CharField(max_length = 20)
number = models.BigIntegerField()
address =models.CharField(max_length = 20)
def __unicode__(self):
return self.name
class Pet(models.Model):
animal = models.CharField(max_length =20)
description = models.TextField()
owner = models.ForeignKey(Store)
image = models.FileField(upload_to="images/",blank=True,null=True)
def __unicode__(self):
return self.animal
This is parts of my views.py
import from django.core.files.uploadedfile import SimpleUploadedFile
def fan(request): # this is the function that saves my form into my models.
form = PetForm(request.POST or None)
if form.is_valid():
dad = form.save(commit=False)
dad.save()
if 'cat' in request.POST:
cat = request.POST['next']
else:
cat = reverse('world:index')
return HttpResponseRedirect(cat)
return render_to_response(
'fan.html',
{'form':PetForm()},
context_instance = RequestContext(request)
)
and My fan.html
<form method="POST" "action">{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input type = "submit" value= "Add Pets to Store" />
</form>
Because your override your Pet model image. Delete the image in your form.
class PetForm(forms.ModelForm):
animal =forms.CharField(max_length = 20,widget = forms.Textarea)
description =forms.CharField(max_length =20, widget = forms.Textarea)
owner = forms.ModelChoiceField(queryset=Store.objects.all())
class Meta:
model = Pet
fields = ('animal','description','owner','image')
//It's not necessary to defined again model field in the form. Once you call the model
//in the form it's understood what you want to show. You can only defined the model
//field again if you want something to add or embed in that field. Like for example you
//want to change the charfield in to textarea or you defined a queryset like you did
//above. Erase your max_length because you are already defined that in the model.
When you upload images don't forget to add "multipart/form-data" in the form
<form method="POST" enctype="multipart/form-data" "action" >
{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input type = "submit" value= "Add Pets to Store" />
</form>
I have two models like this:
class Person(models.Model):
name = models.CharField(max_length=100)
house = models.ForeignKey('House')
class House(models.Model):
address = models.TextField()
Is there some way to create a model form for Person and have it include inline the form to edit the related House object as well? From what I understand of the inline formsets stuff, I would only use that if I have a form editing a House and I want to display forms for all the related Persons. Any ideas?
Just stick the HouseForm into the PersonForm, evaluate it as part of the clean() process, and save it as part of the save() process. Works in a modelformset too.
class HouseForm(forms.modelForm):
""" Edit a house """
class Meta:
model = House
exclude = ()
class PersonForm(forms.ModelForm):
""" Edit a person and her house """
class Meta:
model = Person
exclude = ()
def __init__(self, *args, **kwargs):
super(PersonForm, self).__init__(*args, **kwargs)
self.fields['house'].required = False
data = kwargs.get('data')
# 'prefix' parameter required if in a modelFormset
self.house_form = HouseForm(instance=self.instance and self.instance.house,
prefix=self.prefix, data=data)
def clean(self):
if not self.house_form.is_valid():
raise forms.ValidationError("House not valid")
def save(self, commit=True):
obj = super(PersonForm, self).save(commit=commit)
obj.house = self.house_form.save()
obj.save()
Then in your markup:
<form ...>
{{ person_form }}
{{ person_form.house_form }}
</form>
You have access to the related House object through the Person. As such, I would use the house object as the instance for a Modelform.
HouseForm(ModelForm):
class Meta:
model = House
Say you have a Person object of Paul with a related House.
house_form = HouseForm(instance=Paul.house)
Is this what you were getting at?
I'm not sure whether it is the best way to solve it, but I would do something like this:
Define a ModelForm from each model:
class PersonForm(ModelForm):
class Meta:
model = Person
class HouseForm(ModelForm):
class Meta:
model = House
Define a template like this one, outputting both forms:
<form action="" method="post">
<table>
{{ form1 }}
{{ form2 }}
</table>
<input type="submit">
</form>
A view to create the form to edit the information from both models.
def edit(request):
# You could grab the id from the request that calls the edit form
p = models.Person.objects.get(pk=request.GET.get('id'))
h = models.House.objects.get(pk=p.house.id)
return render_to_response('template.html',
RequestContext(request,
{'form1': PersonForm(instance=p), 'form2': HouseForm(instance=h)}
)
)
And so on.