=============
> Numerous clever workarounds are given in the forums but I decided to
> try using "formfield_for_manytomany" in the inline class. This works
> brilliantly.
==============
Make that
"...using formfield_for_foreignkey() in the inline class."
!!!  (Rushing to go for a coffee!)

On Aug 16, 9:52 am, DelS <[email protected]> wrote:
> I am fairly new and using Django Admin to manage a set of lists and
> forms for a database GUI.
> I am not happy with the performance rendering a form which has
> foreignkey fields or manytomany fields with numerous values. This
> inspired me to get involved with overloading
> "formfield_for_foreignkey" which I eventually managed.
> When I went to tackle a "manytomany" formfield for an 'inline' the
> "admin" doco says "use 'formfield_for_manytomany' as usual, so I
> created the approriate method in the 'inline' Class but it was never
> invoked.
> Numerous clever workarounds are given in the forums but I decided to
> try using "formfield_for_manytomany" in the inline class. This works
> brilliantly.
> I guess the principle is that within the "through" model, the fields
> are actually "foreignkey" fields, and the 'db_field.name' values
> correspond to that model.
>
> Here is a fragment of code where I cache the queryset for multiple
> fields in the session dictionary.
>
> #---------------------------------------------
> # use inline feature in Route for Points and Tracks
> class PointsInline(TabularInline):
>     model = Route.points.through
>     extra = 0
>
> class TracksInline(TabularInline):
>     def formfield_for_foreignkey( self,db_field, request, **kwargs):
>         if db_field.name == "track_id" or db_field.name == "point_id":
>             url_split = request.path.split('/')
>             route_id = url_split[-2]
>             try:
>                 previous_id = request.session.get("RouteId")
>             except:
>                 previous_id = None
>             if route_id <> previous_id :
>                 print db_field.name, request.path, "\n"
>                 request.session["RouteId"] = route_id
>                 q_line =
> Route.objects.get(id=route_id).from_signal.location.line
>                 tq_set = Track.objects.filter(location__line=q_line)
>                 request.session["TrackList"] = tq_set
>                 pq_set = Point.objects.filter(location__line=q_line)
>                 request.session["PointList"] = pq_set
>                 print q_line, tq_set.count(), pq_set.count()
>             if db_field.name == "track_id":
>                 q_set = request.session["TrackList"]
>             else:
>                 q_set = request.session["PointList"]
>             kwargs["queryset"] = q_set
>             return super(TracksInline,
> self).formfield_for_foreignkey(db_field, request, **kwargs )
>         else:
>             return None
>     model = Route.tracks.through
>     extra = 0
>
> class RouteAdmin(ModelAdmin):
>     inlines = [ PointsInline, TracksInline ]
>     list_per_page = 48
>     list_display =
> ('__unicode__','from_signal','to_signal','direction')
>     list_filter= ('to_signal__location__line',)
>     exclude = ('tracks','points',)
>     search_fields =
> ('to_signal__location__vcode','from_signal__location__vcode',)
> #---------------------------------------------

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


Reply via email to