Thanks Rajesh - this works perfectly!
I now have another question...
What do I now do about manytomany fields for example look at the
contributor field in the example below - I want the current
contributors to be selected in the change form (these can obviously be
more than one)
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()
contributor=models.ManyToManyField(Designer)
class Priority(models.Model):
priority=models.CharField(max_length=20)
class Designer(models.Model):
first_name = models.CharField(max_length=100)
last_name=models.CharField(max_length=100)
email=models.EmailField(unique=True)
def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
form.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})
)
contributor = forms.ModelMultipleChoiceField(
label='Contributor',
queryset=Designer.objects.all()
)
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
contributor = designset.contributor
form = SmallDesignSetSaveForm(initial={
'priority': priority.pk,
'status': status.pk,
'title': title,
'concept': concept,
'contributor': contributor,
})
Many thanks,
Martin
On Jul 14, 8:48 pm, Rajesh Dhawan <[EMAIL PROTECTED]> wrote:
> 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
-~----------~----~----~----~------~----~------~--~---