Django Form with Foreign Key Related Data -


i having issues having form have user select value rather typing text box. trying create tool checkout transaction, requires toolid, quantity of tool checked out, , partyid. let user able see list of users first , last names rather select partyid. first , last names required when make form containing fields gives me text box input. believe rather have user select desired user drop down option. appreciated.

models:

class party(models.model): partyid=models.autofield(primary_key=true, db_column='partyid') firstname=models.charfield(max_length=100, null=false) lastname=models.charfield(max_length=100, null=false) phonenumber=models.charfield(max_length=25, null=false) organization=models.charfield(max_length=100, null=true) deleted=models.booleanfield(default=0)  objects=models.manager()  class meta:     managed=true     db_table='party'  def __unicode__(self):     return str(self.partyid)  def get_absolute_url(self):     return reverse("toolsearch:borrowerupdate", kwargs={"pk": self.partyid})  class tooltransaction(models.model): checkoutid=models.autofield(primary_key=true) toolid=models.foreignkey(tool, db_column='toolid', on_delete=models.cascade,) partyid=models.foreignkey(party, db_column='partyid', on_delete=models.cascade,) quantity=models.integerfield(null=false) checkoutdate=models.datefield(null=false, default=datetime.datetime.now) checkindate=models.datefield(null=true) deleted=models.booleanfield(default=0) objects=activetransactionmanager() #objects=models.manager()  class meta:      managed=true     db_table='tooltransaction'     ordering=('checkoutid',)  def __unicode__(self):     return str(self.checkoutid)  def get_absolute_url(self):     return reverse("toolsearch:toolcheckin", kwargs={"pk": self.checkoutid})  class tool(models.model): toolid=models.charfield(max_length=100, primary_key = true, unique=true, db_column='toolid') quantity=models.integerfield(null=false) location=models.charfield(max_length=100, null=false) categoryid=models.integerfield(null=true) deleted=models.booleanfield(default=0)  objects=models.manager()  class meta:         managed=true         db_table='tool'  def __unicode__(self):     return self.toolid  def get_absolute_url(self):     return reverse("toolsearch:toolsearchresults", kwargs={"pk": self.toolid}) 

forms:

class borrowerselectform(forms.modelform): class meta:     model = party     fields = [         "firstname",         "lastname",         "phonenumber"     ]  class toolcheckoutcreateform(forms.modelform): class meta:     model = tooltransaction     fields = [         "toolid",         "quantity",         "partyid",     ] 

view:

def tool_checkout(request):     queryset = party.objects.all()     form = toolcheckoutcreateform(request.post or none)     form2 = borrowerselectform(request.post or none)     if form.is_valid():         instance=form.save(commit=false)         instance.save()         messages.success(request, "tool checked out succesfully")     context={         "form": form,         "form2": form2     }     return render(request, "toolcheckout.html", context) 

i fine using 1 form if can have toolcheckoutcreateform reference objects party model opposed having 2 forms displayed @ once. not sure best method go this.

thanks!

i changed approach , ended solving problem. incorrect in how using models. making 1 model per database table. however, needed make 1 model per task performed. instance, checkin , checkout both use tooltransaction model. making checkin model (identical tooltransaction model above), , checkout model able add reverse lookups correct url/view complete action. combined html built form placed data inside , made un-editable data retrieved queryset in view. continued use form toolcheckoutcreateform. html form held borrower information.


Comments