Note that there are some explanatory texts on larger screens.

plurals
  1. POsearch_fields in Django behaved strangely in Django ModelAdmin
    primarykey
    data
    text
    <p>I have a django model admin page and wanna use its search function so I defined a search_fields like this:</p> <pre><code> search_fields = ( 'name', 'email', 'vacancy__position__name', 'vacancy__location__name', ) </code></pre> <p>Problem is the 'vacancy' field is nullable, but the search can not find items with null 'vacancy', so I dig into the sql it executed and found the problem:</p> <pre><code>SELECT "testapp_subject"."name", "testapp_subject"."email", FROM "testapp_candidate" INNER JOIN "testapp_subject" ON ("testapp_candidate"."subject_ptr_id" = "testapp_subject"."id") LEFT OUTER JOIN "testapp_vacancy" ON ("testapp_candidate"."vacancy_id" = "testapp_vacancy"."id") LEFT OUTER JOIN "testapp_position" ON ("testapp_vacancy"."position_id" = "testapp_position"."id") INNER JOIN "testapp_location" ON ("testapp_vacancy"."location_id" = "testapp_location"."group_ptr_id") WHERE (UPPER("testapp_subject"."name"::text) LIKE UPPER(%j%) OR UPPER("testapp_subject"."email"::text) LIKE UPPER(%j%) OR UPPER("testapp_position"."name"::text) LIKE UPPER(%j%) OR UPPER("testapp_location"."name"::text) LIKE UPPER(%j%) ) ORDER BY "testapp_candidate"."subject_ptr_id" DESC </code></pre> <p>As you can see, the last INNER JOIN should be LEFT OUTER JOIN.</p> <p>I tried to dig into the sql generating part to see what's going on but the code is too complicated to read. Anyone has a clue on what's going on or is there anyone ran into the same situation?</p> <p>Following is a simplified model structure for my case.</p> <pre><code>class Group(models.Model): pass class Organization(models.Model): name = models.CharField(max_length = 50) class Position(models.Model): name = models.CharField(max_length=80) class Location(Group): name = models.CharField(max_length=80) org = models.ForeignKey(Organization) class Vacancy(models.Model): position = models.ForeignKey(Position) location = models.ForeignKey(Location) date = models.DateField(default=datetime.now()) filled = models.BooleanField(default=False) class Subject(models.Model): org = models.ForeignKey(Organization, verbose_name="organization") name = models.CharField(max_length = 50) email = models.CharField(max_length = 85, null=True, blank=True) class Candidate(Subject): vacancy = models.ForeignKey(Vacancy, null=True, blank=True) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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