Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two examples only slightly different:</p> <p>First:</p> <pre><code>class ScanInlineAdmin(admin.TabularAdmin): model = Scan formset = ScanInlineFormset def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name == "visit": patient = self.get_object(kwargs['request'], Patient) kwargs["queryset"] = Visit.objects.filter(patient=patient) return super(ScanInlineAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) def get_object(self, request, model): object_id = request.META['PATH_INFO'].strip('/').split('/')[-1] try: object_id = int(object_id) except ValueError: return None return model.objects.get(pk=object_id) </code></pre> <p>Second:</p> <pre><code>class ScanInline(admin.TabularInline): model = Scan formset = ScanInlineFormset def formfield_for_dbfield(self, field, **kwargs): if field.name == 'visit': # Note - get_object hasn't been defined yet patient = self.get_object(kwargs['request'], Patient) vists = Visit.objects.filter(patient=patient) return forms.ModelChoiceField(queryset=visits) return super(ScanInline, self).formfield_for_dbfield(field, **kwargs) def get_object(self, request, model): object_id = request.META['PATH_INFO'].strip('/').split('/')[-1] try: object_id = int(object_id) except ValueError: return None return model.objects.get(pk=object_id) </code></pre> <p>You may find this <a href="http://www.stereoplex.com/blog/filtering-dropdown-lists-in-the-django-admin" rel="nofollow">article</a> helpful. I assume your in the patient page admin edit/view where you want to enter the new scan. Is that correct?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload