Data not showing in select field

I’m stuck on this problem for quite awhile. I cannot get a select field to show the data.

Here’s the code:

models.py:

class SetList(models.Model):

	id = models.CharField(primary_key=True, max_length=10)
	set_name = models.CharField(max_length=80)
	
	def __str__(self):
		return(f"{self.id} {self.set_name}")
		return self.id
	
	class Meta:
		ordering = ['id']
		indexes = [
        	models.Index(fields=['id', 'set_name']),
        ]

views.py:

def add_record(request):

		sets = SetList.objects.all()
		form = SelectSetForm(request.POST)
		context = {'form':form, 'sets':sets}
		return render(request, 'add_record.html', context)

forms.py:

class SelectSetForm(forms.ModelForm):

	class Meta:
		model = SetList
		fields = ('id', 'set_name')
		labels = {'id':'ID', 'set_name': 'SN'}
		widgets = {'id': forms.Select(attrs={"placeholder": "Set", "class":"form-select"}),
                  'set_name':forms.Select(attrs={"placeholder":"set_name", "class":"form-select"}),
                  
                  }

and the html:

<form class="row g-3" action="{% url 'add_record' %}" method="POST" enctype="multipart/form-data">
	{% csrf_token %}
<div class="col-md-6">
	
	
	{{ form.as_p }}
	
</div>

I get a select field on the page, but it’s blank. There is data in the dbase. What have I messed up? Thanks.

You are not providing any list of choices for either select field. You would need to define the fields as ChoiceField and define the list of choices available.

(Although, I can’t understand why you would ever want a select field on your primary key. You can’t change the primary key of an instance of a model. Saving an instance of a model with a primary key either updates that other instance of the model or inserts a new row.)

If you provided more details about what the real situation is, we might be able to offer more tangible suggestions.

Your answer is confusing me. I have used the Select widget in other forms in order to fill in data from other models. I thought the ‘choices’ came from the entries in the table from the model.

Also, suppose I wish to update a record. Why would I not want to select the record from its primary key?

In any case, what I’m really trying to do here is a bit complicated. I have 2 models, not listed here, one of which has a foreign key to the model above. I wish to have a form to add a record to this model, and I wish to pre-filter the options by having the user select an entry from the model above. Otherwise, it wants to retrieve some 70000 records from which to choose. If the user selects an option from the model above, it narrows the choices to roughly 300. I thought this would be a decent approach, and I still don’t understand why my select field is not displaying any of the data from the id field.

Briefly, choices are created from a model when the field you are creating the form field for is a foreign key to another model. In that situation, Django can build the choice list from the entries in the referent table. However, when you’re creating a field that is not a foreign key, Django has no way of implicitly determining what the choices are.

Please show the code of one of those other examples that you are referencing, and I can show you the difference.

Also, when describing your situation, please try to avoid using indefinite articles such as “this model” or “the model above”. Use the actual model name to avoid ambiguity. (It would be helpful if you either edited your description above to replace the descriptive terms with the actual model names involved, or rewrite it as a new response.)

That explains it perfectly. I was using the primary key of one model when I should have been using the foreign key of the other model. Now it does what I want.

I will remember your suggestions for the next time (I’m sure there will be one). Thank you very much.