Custom queryset on a ModelForm field

2,079 views
Skip to first unread message

Simon W

unread,
Jan 4, 2011, 2:42:16 PM1/4/11
to django...@googlegroups.com
Hi,

I have a 'project' model

class Project(models.Model):
    title = models.CharField(max_length=100)
    links = models.ManyToManyField('Link', related_name='link')

and a 'link' model

class Link(models.Model):
    label = models.CharField(max_length=50)
    url = models.URLField(max_length=100)
    project = models.ForeignKey(Project)

I want the user to edit his project properties. I'm using ModelForm class and a UpdateView to put everything together. But I have a problems:

I want the form to only show those links who are owned by the project in the MultipleChoisesWidget, hence the ForeignKey in the Link model.

Any help on how to achieve this? Thanks!

P.S why isn't there a proper API documentation on django like doxygen? I find the django documentation full of content but awful structure!

Daniel Roseman

unread,
Jan 4, 2011, 3:22:12 PM1/4/11
to django...@googlegroups.com
class ProjectForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ProjectForm, self).__init__(*args, **kwargs) 
        if self.instance and self.instance.pk:
            self.fields['links'].queryset = Link.objects.filter(project=self.instance)

    class Meta:
        model = Project

-- 
DR.

mrmclovin

unread,
Jan 5, 2011, 6:35:14 AM1/5/11
to Django users
Thanks, that did the trick!

On Jan 4, 9:22 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Tuesday, January 4, 2011 7:42:16 PM UTC, mrmclovin wrote:
>
> > Hi,
>
> > I have a 'project' model
>
> > class Project(models.Model):
> >     title = models.CharField(max_length=100)
> >     links = models.ManyToManyField('Link', related_name='link')
>
> > and a 'link' model
>
> > class Link(models.Model):
> >     label = models.CharField(max_length=50)
> >     url = models.URLField(max_length=100)
> >     project = models.ForeignKey(Project)
>
> > I want the user to edit his project properties. I'm using ModelForm class
> > and a UpdateView to put everything together. But I have a problems:
>
> > I want the form to only show those *link*s who are owned by the project in

derek

unread,
Jan 7, 2011, 9:08:23 AM1/7/11
to Django users
Re: "why isn't there a proper API documentation on django like
doxygen? I find the django documentation full of content but awful
structure! "

Have a look at:
http://docs.djangoproject.com/en/dev/internals/documentation/

I am curious as to what specific problems you have? I have read the
docs for many open source (and closed source) projects over the last
dozen years and rate Django's docs as some of the best I have seen.

In addition, bear in mind this is a open source project: if there are
demonstrably significant improvements you can make, I am sure they
will be appreciated!

On Jan 4, 9:42 pm, Simon W <simw...@gmail.com> wrote:
> Hi,
>
> I have a 'project' model
>
> class Project(models.Model):
>     title = models.CharField(max_length=100)
>     links = models.ManyToManyField('Link', related_name='link')
>
> and a 'link' model
>
> class Link(models.Model):
>     label = models.CharField(max_length=50)
>     url = models.URLField(max_length=100)
>     project = models.ForeignKey(Project)
>
> I want the user to edit his project properties. I'm using ModelForm class
> and a UpdateView to put everything together. But I have a problems:
>
> I want the form to only show those *link*s who are owned by the project in
Reply all
Reply to author
Forward
0 new messages