Custom queryset on a ModelForm field

2011-01-04 Thread Simon W
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
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!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Custom queryset on a ModelForm field

2011-01-04 Thread Daniel Roseman
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 
> 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!
>


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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Custom queryset on a ModelForm field

2011-01-05 Thread mrmclovin
Thanks, that did the trick!

On Jan 4, 9:22 pm, Daniel Roseman  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
> > 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!
>
> 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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Custom queryset on a ModelForm field

2011-01-07 Thread derek
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  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
> 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!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.