Currently, I have some data stored in a Django model with the following structure:
#Django model
from django.db import models
class Customer(models.Model):
first_name = models.Charfield(max_length = 120, null = False, blank = False)
last_name = models.Charfield(max_length = 120, null = False, blank = False)
def __str__(self):
return self.first_name+ " " + self.last_name
I want to display all the customers that are stored in the Customer model in the DropDown menu form. What I tried was the following but I've got no success:
#Django form
from .models import Customer
# This throws as a result <Django.db.models.query.utils.deferred.attribute at 0x00013F> inside of the DropDown Menu form
class DropDownMenuCustomer(forms.Form):
Customer = forms.ChoiceField(choices=[(x,x) for x in str(Customer.first_name)+str(" ")+ str(Customer.last_name))])
Another possibility without success was:
#Django form
from .models import Customer
class DropDownMenuCustomer(forms.Form):
querySet = Customer.objects.all()
list_of_customers = []
for customer in querySet:
list_of_customers.append(customer.first_name + " " + customer.last_name)
customer= forms.ChoiceField(choices=[(x) for x in list_of_customers)])
This last possibility does not find list_of_customers, just as if the variable has not been declared. How can I display the data stored in the Django models in the Django forms?