Hi Martin,
On Jul 14, 12:14 pm, mbdtsmh <[EMAIL PROTECTED]> wrote:
> Hi everyone - wondering if someone could point me in the right
> direction with this one?
>
> I am trying to populate fields in a form with data from the database
> (i.e., a change form rather than an add new form). It works fine for
> populating Char/Date Fields but when I do the same for ForeignKey or
> m2m fields I loose the information about the items that have been
> selected. I.e., for a foreign key I get the dropdown for the foreign
> key list but no single item is highlighted as selected.
>
> Below is an example:
>
> models.py:
>
> class SmallDesignSet(models.Model):
> title=models.CharField(max_length=100)
> priority=models.ForeignKey('Priority')
> status=models.ForeignKey(Status, default=2)
> concept=models.DateField()
>
> class Priority(models.Model):
> priority=models.CharField(max_length=20)
> ...
>
> forms.py:
>
> class SmallDesignSetSaveForm(forms.Form):
> priority = forms.ModelChoiceField(
> label='Priority',
> queryset=Priority.objects.all()
> )
> status = forms.ModelChoiceField(
> label='Status',
> queryset=Status.objects.all()
> )
> title = forms.CharField(
> label='Title',
> widget=forms.TextInput(attrs={'size':64})
> )
> concept = forms.DateField(
> label='Concept',
> widget=forms.TextInput(attrs={'size':10})
> )
>
> views.py:
>
> def smalldesignset_save_page(request):
> ...
> elif request.GET.has_key('id'):
> id = request.GET['id']
> designset = SmallDesignSet.objects.get(pk=id)
> priority = designset.priority
> status = designset.status
> title = designset.title
> concept = designset.concept
> form = SmallDesignSetSaveForm(initial={
> 'priority': priority,
> 'status': status,
> 'title': title,
> 'concept': concept,
> })
Try:
form = SmallDesignSetSaveForm(initial={
'priority': priority.pk,
'status': status.pk,
'title': title,
'concept': concept,
})
-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---