Sykes
Sykes

Reputation: 9

Filling django select field

So, I'm trying to implement Select widget in Django Form, to input into OneToOne area in Model.It appeared, but a problem is don't know how to fill it with initial data.

models.py

class Check(models.Model):
    name = models.CharField(max_length = 100)
    def __unicode__(self):
         return self.name

class Check_date(models.Model):
    date = models.DateTimeField()
    check = models.ForeignKey(Check)

    def __unicode__(self):
         return '%s' % (self.date)

forms.py

class Check_DateForm(forms.Form):
date = forms.DateTimeField(label = 'Time of Check', widget  = forms.DateTimeInput)
check_id = forms.ChoiceField(label = 'Check ID', widget = forms.Select)

So, there gotta be some choices, leading to Check model. I'm talking about this input.

check_id = forms.ChoiceField(label = 'Обход', widget = forms.Select)

Upvotes: 0

Views: 5051

Answers (2)

Chris Hawkes
Chris Hawkes

Reputation: 12410

When calling your form object into your view you can prepopulate the fields using initial.

 def view(request):
        game = Game.objects.get(id=1) # just an example
        data = {'id': game.id, 'name': game.name}
        form = UserQueueForm(initial=data)
        return render_to_response('my_template.html', {'form': form})

When the form gets put into the template it will have those fields pre-filled.

Upvotes: 0

andrean
andrean

Reputation: 6796

you should use a ModelChoiceField, and pass it a queryset, like this:

check_id = forms.ModelChoiceField(queryset=Check.objects.all())

Upvotes: 2

Related Questions