On Jul 16, 10:52 am, mbdtsmh <[EMAIL PROTECTED]> wrote:
> 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)
See below...
>
> 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
Insert this line here:
contributor_list = [c.pk for c in contributor]
> form = SmallDesignSetSaveForm(initial={
> 'priority': priority.pk,
> 'status': status.pk,
> 'title': title,
> 'concept': concept,
> 'contributor': contributor,
Replace the above with:
'contributor':contributor_list
> })
Also, as a matter of good practice, you should consider naming
ManyToManyFields in plural. So, in the above case, the field would be
SmallDesignSet.contributors instead of SmallDesignSet.contributor.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---