Thank you. The code fixed my problem.
On Dec 16, 2009, at 1:48 PM, Tim Valenta wrote: > Well, you're probably getting the error on your 'extra' inlines. > self.instance is unbound in that case, and so self.instance.providers > cannot be looked up. > > I wrote a little thing to fetch a proper object though... just be sure > to include your parent model's field in your fields on the inline. > Then you can use this generic utility function... you can pick pieces > out of it if you think it wiser. I just took the sledgehammer to the > wall to get my task accomplished: > > def implied_inline_fk(form, implied_model_class, field_name=None): > if not field_name: > field_name = implied_model_class.__name__.lower() > try: > return implied_model_class.objects.get(pk=tuple(i[0] for i in > form.fields[field_name].widget.choices)[1]) > except IndexError: > # In the event that both the base 'original' object and inline > object don't exist, > # we need to just throw out the possibility of filtering values. > return None > except KeyError: > raise KeyError("'%s' must be included in the inline ModelAdmin's > 'fields' attribute." % field_name) > > > > You could then use this in your PartnerForm: > > class PartnerForm(forms.ModelForm): > def __init__(self, *args, **kwargs): > super(PartnerForm, self).__init__(*args, **kwargs) > if 'instance' in kwargs: > # Existing instance-- safe to use > self.fields['default_provider'].queryset = > self.instance.providers > else: > # 'extra' unbound object. Not safe. > # Must derive through some other means. > parentInstance = implied_inline_fk(self, PARENTMODEL, > 'field_name_to_parent_model') > self.fields['default_provider'].queryset = > parentInstance.somelookup_to_provider_filter > > Note that sometimes this doesn't really work, depending on your model > hierarchy. You might have to throw in the towel on 'extra' inlines. > The key is to filter only when it's bound, and to work around the > filter if it's not bound. > > Tim > > > On Dec 16, 11:10 am, Eric Chamberlain <e...@rf.com> wrote: >> I'd like PartnerRegistration's default_provider field to be filtered in >> PartnerRegistrationInline, like how Partner's default_provider field is >> filtered in PartnerAdmin. >> >> When I try the code below, I get a DoesNotExist exception in: >> >> self.fields['default_provider'].queryset = >> self.instance.partner.providers >> >> raise self.field.rel.to.DoesNotExist >> >> What am I doing wrong? >> >> class Partner(Model): >> id = UUIDField(primary_key=True) >> providers = ManyToManyField('Provider', >> related_name='provider_partners', blank=True, null=True) >> default_provider = ForeignKey('Provider', null=True, blank=True, >> help_text='The calling service used to make calls through by default when >> using this partner') >> >> class PartnerForm(forms.ModelForm): >> """ Filter and only show what belongs to this partner.""" >> >> def __init__(self, *args, **kwargs): >> super(PartnerForm, self).__init__(*args,**kwargs) >> self.fields['default_provider'].queryset = self.instance.providers >> >> class PartnerRegistration(Model): >> id = UUIDField(primary_key=True) >> partner = ForeignKey('Partner',unique=True) >> default_provider = ForeignKey('Provider',verbose_name='default calling >> service', help_text='User this calling service when response doesn\'t >> specify one.') >> >> class PartnerRegistrationForm(forms.ModelForm): >> """ Filter and only show what belongs to this partner.""" >> >> def __init__(self, *args, **kwargs): >> super(PartnerRegistrationForm, self).__init__(*args,**kwargs) >> self.fields['default_provider'].queryset = >> self.instance.partner.providers >> >> class PartnerRegistrationInline(StackedInline): >> model = PartnerRegistration >> form = PartnerRegistrationForm >> >> class PartnerAdmin(ModelAdmin): >> form = PartnerForm >> >> inlines = [ PartnerRegistrationInline, ] > > -- > > 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.