models.py
Create your models here.
class DeviceType(models.Model):
device_type = models.CharField(max_length=200,unique=True)
class Repair(models.Model):
device_type = models.ForeignKey(DeviceType,to_field='device_type')
submitted_by = models.ForeignKey(User,to_field='username')
comments = models.TextField()
repair_request_date = models.DateField(default=datetime.now)
repair_status = models.ForeignKey(RepairStatus, to_field="repair_status", null=True)
repair_cost_estimate = models.FloatField(null=True)
views.py
def repair_form(request):
dlist = get_my_choices(request.user)
if request.method == "POST":
form = RepairForm(request.POST,device_type = dlist, submitted_by = request.user)
#print(form)
if form.is_valid():
print("Inside repair form")
post = form.save(commit=False)
post.submitted_by = request.user
print(post.pk)
post.save()
return redirect("RepairRequest")
else:
dlist = get_my_choices(request.user)
print("Inside else of repair form")
form = RepairForm( device_type = dlist, submitted_by = request.user)
print("else form: ", form)
return render(request, "Form_template.html", {"form": form,\
"name": "Repair Request Form"})
forms.py
class RepairForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
print("kwargs: ",kwargs)
dlist = kwargs["device_type"]
print("Inside init of RequestForm")
super(RepairForm, self).__init__(*args, **kwargs)
self.fields["device_type"] = forms.ChoiceField(choice = [dlist])
self.fields["submitted_by"] = kwargs["submitted_by"]
class Meta:
print("Inside Meta of RepairForm")
model = Repair
print("model: ", model)
fields = "__all__"
print("fields: ", fields)
exclude = ("repair_cost_estimate","repair_status","submitted_by","repair_request_date",)
I am trying to create a dynamic dropdown list in django forms. Dynamic in the sense that the dropdown list will only populate with the data that is associated with the given user. In my case, each user may have a laptop, mouse, etc. with him. I have written the code shown above. Every time I execute the code I get the following error:
TypeError at /user_dashboard/RepairRequest.html
__init__() got an unexpected keyword argument 'submitted_by'
Request Method: GET
Request URL: http://127.0.0.1:8000/user_dashboard/RepairRequest.html
Django Version: 1.11.13
Exception Type: TypeError
Exception Value:
__init__() got an unexpected keyword argument 'submitted_by'
Exception Location: E:\inventory management\inventory_management_service\inventory_management_app\forms.py in __init__, line 15
Python Executable: C:\Anaconda2\python.exe
Python Version: 2.7.14
Python Path:
['E:\\inventory management\\inventory_management_service',
'C:\\Anaconda2\\python27.zip',
'C:\\Anaconda2\\DLLs',
'C:\\Anaconda2\\lib',
'C:\\Anaconda2\\lib\\plat-win',
'C:\\Anaconda2\\lib\\lib-tk',
'C:\\Anaconda2',
'C:\\Users\\Jaimik Jain\\AppData\\Roaming\\Python\\Python27\\site-packages',
'C:\\Anaconda2\\lib\\site-packages',
'C:\\Anaconda2\\lib\\site-packages\\win32',
'C:\\Anaconda2\\lib\\site-packages\\win32\\lib',
'C:\\Anaconda2\\lib\\site-packages\\Pythonwin',
'C:\\Anaconda2\\lib\\site-packages\\pywinpty-0.5-py2.7-win-amd64.egg']
Server time: Thu, 7 Jun 2018 09:56:44 +0000
I request your help at the earliest.
popsubmitted_bybefore performing asuper().__init__call.