Properly using Foreign Key references in search_fields, Django admin
Asked Answered
H

1

8

I've got a weird conundrum that I need some help with in Django 1.8.4 using python 3.4 in a virtual-env.

I've got 2 models in 2 different apps... as follows with multiple Foreign Key references.

Inventory App

class InventoryItem(models.Model):
    item_unique_code = models.CharField(max_length=256, blank=False, null=False)
    category = models.CharField(max_length=256, blank=False, null=False,choices=[('RAW','Raw Material'),('FG','Finished Good'),('PKG','Packaging')])
    name = models.CharField(max_length=64, blank=False, null=False)
    supplier = models.CharField(max_length=96, blank=False,null=False)
    approved_by = models.CharField(max_length=64, editable=False)
    date_approved = models.DateTimeField(auto_now_add=True, editable=False)
    comments = models.TextField(blank=True, null=True)

    def __str__(self):
        return "%s | %s | %s" % (self.item_unique_code,self.name,self.supplier)

    class Meta:
        managed = True
        unique_together = (('item_unique_code', 'category', 'name', 'supplier'),)

Recipe App

class RecipeControl(models.Model):
    #recipe_name choice field needs to be a query set of all records containing "FG-Finished Goods"
    recipe_name = models.ForeignKey(items.InventoryItem, related_name='recipe_name', limit_choices_to={'category': 'FG'})
    customer = models.ForeignKey(customers.CustomerProfile, related_name='customer')
    ingredient = models.ForeignKey(items.InventoryItem, related_name='ingredient')
    min_weight = models.DecimalField(max_digits=16, decimal_places=2, blank=True, null=True)
    max_weight = models.DecimalField(max_digits=16, decimal_places=2, blank=True, null=True)
    active_recipe = models.BooleanField(default=False)
    active_by = models.CharField(max_length=64, editable=False)
    revision = models.IntegerField(default=0)
    last_updated = models.DateTimeField(auto_now_add=True, editable=False)

    def __str__(self):
       return "%s" % (self.recipe_name)

    class Meta:
        managed = True
        unique_together = (('recipe_name', 'customer', 'ingredient'),)

I've been getting some weird results in my Recipe's Admin class...

from django.contrib import admin
from django.contrib.auth.models import User
from .models import RecipeControl
from Inventory import models

class RecipeView(admin.ModelAdmin):
    def save_model(self, request, obj, form, change): 
        obj.active_by = request.user.username
        obj.save()

    fieldsets = [
        ('Recipe Information',               {'fields': ['recipe_name', 'customer']}),
        ('Ingredients', {'fields': ['ingredient','min_weight','max_weight','active_recipe']}),
        ('Audit Trail', {'fields': ['active_by','revision','last_updated'],'classes':['collaspe']}),
    ]

    list_select_related = ['recipe_name','customer','ingredient']
    search_fields = ['recipe_name','customer','ingredient','active_by']
    readonly_fields = ('last_updated','active_by')
    list_display = ['recipe_name','customer','ingredient','min_weight','max_weight','last_updated','active_by', 'active_recipe']

admin.site.register(RecipeControl, RecipeView)

The issue I've come across is if I try to do a search on any ForeignKey field, Django throws this error...

Exception Type: TypeError at /admin/Recipe/recipecontrol/
Exception Value: Related Field got invalid lookup: icontains

According to the Django Admin Doc's and other older questions on stackoverflow on the subject it says I should be doing something along the lines of search_fields = ['inventoryitem__name'] but I think this is in reference to FK's in the same app model.py. Is there a more correct way of referencing/importing other models from other apps that I'm missing or do I have to use some kind of callable method magic to get the search function to look up correctly? I've tried a multitude of different combinations and nothing seems to work. I'm relatively new to Django so I'm confident it's something simple.

Hush answered 1/2, 2016 at 11:50 Comment(0)
F
13

You should use the double underscore notation to search a field on a related object. However, you should use the name of the foreign key field (e.g. recipe_name), not the name of the model (e.g. InventoryItem). It doesn't matter whether or not the foreign key's model is in the same app. For example:

search_fields = ['recipe_name__name']

Note that if you want to search the recipe_name and ingredient fields, you need to include both fields, even though they are foreign keys to the same model.

search_fields = ['recipe_name__name', 'ingredient__name']
Fledgy answered 1/2, 2016 at 12:0 Comment(1)
Thank so much for clearing this up! The Django docs didn't elaborate enough for me to get the message.Hush

© 2022 - 2024 — McMap. All rights reserved.