One possible way seems to override the render_change_form in AdminClass as pointed in this post: http://groups.google.com/group/django-users/browse_thread/thread/94bdf5d7eeefc99b/9cbdb25672387c33?lnk=gst&q=select+foreign+key+list#9cbdb25672387c33
On Jan 12, 8:34 pm, GuyBowden <guy.bow...@gmail.com> wrote: > Hi, > > I am writing a resource booking system in django using the admin > functionality. > > I have a "Booking" model, a "ResourceType" model, and a "Resource" > model. > > The booking model has date from and to fields, aforeignkeyfield toselectthe > ResourceType, and a ManyToManyField to link to the Resource > model. > > I would like the user to be able to enter the dates to and from, andselectthe > resource type. Then from these choices this would only show > those resources available that belong to the specific resource type. > > I have defined an "is_available" method on the Resource, and am able > to pass to and from dates and return True/False if they are available. > > My problem is updating the form with this limited set of Resources to > choose from. > > I am happy to press "Save and continue editing" in order to process > the dates (no need for ajax) > > My question is how to get the dates and resource type in to my form > Model and then into the admin.... > > Any pointers as to how to do this? > > My attempt below - fails :( > > class BookingAdminForm(forms.ModelForm): > > def __init__(self, *args, **kwargs): > super(BookingAdminForm, self).__init__(*args, **kwargs) > > if self.is_valid(): > resourcesChoices = > self.getAvailableResources(self.cleaned_data > ['booked_from'], self.cleaned_data['booked_until']) > self.fields['resources'] = forms.MultipleChoiceField > (choices=resourcesChoices) > else: > pass > #del self.fields['resources'] > > def getAvailableResources(self, booked_from, booked_until): > allResources = Resource.objects.all() > choices = ((0,'none',),) > for resource in allResources: > if resource.is_available(booked_from, booked_until): > choices += ((resource.id, resource.name,),) > return choices > > class Meta: > model = Booking > > class BookingAdmin(admin.ModelAdmin): > list_display = ('customer', > 'resource_type','number_of_resources','booked_from','booked_until',) > list_filter = ('resource_type', 'created_on',) > date_hierarchy = 'booked_from' > search_fields = ['group_leader'] > filter_horizontal = ('resources',) > form = BookingAdminForm > > inlines = [NoteInline] > save_on_top = True --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---