Sorry, I can't reproduce your error with django 1.2.

I copied your models (just removed the intermediaty "Permission"
because I don't know your User model) into a new app and I have 2
scenarios Adding an Application in the admin:

a) if I select a User and Folder, the validation runs just fine
b) if I do not select an User, then the "clean" method is still
accessed, but since 'users' is not present in the cleaned_data dict it
throws an exception - which is the scenario you give, hence my
original guess :)

Nuno

On Wed, Jul 14, 2010 at 2:32 PM, Heleen <heleen...@gmail.com> wrote:
> Thank you for your reply.
>
> Yes I have selected a user and folder.
> I believe ManyToMany fields are always optional (hence many to many).
> So my users and company fields are optional, but my folder field
> isn't.
> When I add a new Application in the Admin I do specify a folder and
> users (if I don't I would at least get a 'Required field' error for
> the folder field).

> I've tested the method above (clean function) with another model that
> has a manytomany field, and it does exactly the same thing, even when
> I really do fill in the data. In fact, if I delibirately throw an
> error and look in the debug info I can see the ManyToMany field data
> being present in the POST data, just like I can when I do the same
> thing with the above models.
>
> Btw, I just noticed a typo in my Folder model description above,
> that's not the issue as it's correct in my original model.
>
> On Jul 14, 2:02 pm, Nuno Maltez <nuno.li...@gmail.com> wrote:
>> Hi,
>>
>> Just a guess: have you actually selected a user and a folder when
>> submitting the form? I think only valid field are present on the
>> cleaned_data dict, and your users and folder fields are not optional
>> (blank=True, null=True).
>>
>> hth,
>> nuno
>>
>> On Tue, Jul 13, 2010 at 1:45 PM, Heleen <heleen...@gmail.com> wrote:
>> > Hello,
>>
>> > I have the following classes:
>> > class Application(models.Model):
>> >  users = models.ManyToManyField(User, through='Permission')
>> >  folder = models.ForeignKey(Folder)
>>
>> > class Folder(models.Model):
>> >  company = models.ManyToManyField(Compnay)
>>
>> > class UserProfile(models.Model):
>> >  user = models.OneToOneField(User, related_name='profile')
>> >  company = models.ManyToManyField(Company)
>>
>> > Now when I save application, I would like to check if the users do not
>> > belong to the application's folder's companies.
>> > I have posed this question before and someone came up with the
>> > following sollution:
>> > forms.py:
>> > class ApplicationForm(ModelForm):
>> >    class Meta:
>> >        model = Application
>>
>> >    def clean(self):
>> >        cleaned_data = self.cleaned_data
>> >        users = cleaned_data['users']
>> >        folder = cleaned_data['folder']
>> >        if
>> > users.filter(profile__company__in=folder.company.all()).count() > 0:
>> >            raise forms.ValidationError('One of the users of this
>> > Application works in one of the Folder companies!')
>> >        return cleaned_data
>>
>> > admin.py
>> > class ApplicationAdmin(ModelAdmin):
>> >    form = ApplicationForm
>>
>> > This seems like right the way to go about this. The problem is that
>> > neither the users nor folder fields are in the cleaned_data and I get
>> > a keyerror when it hits the users = cleaned_data['users'] line.
>>
>> > I was hoping that someone here could explain to me why these
>> > manytomany fields don't show up in cleaned_data and that someone could
>> > possibly give me a sollution to my problem.
>>
>> > Thanks!
>> > Heleen
>>
>> > --
>> > 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 
>> > athttp://groups.google.com/group/django-users?hl=en.
>>
>>
>
> --
> 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.
>
>

-- 
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.

Reply via email to