#3345: Admin Search Fields From Related Models
---------------------------------------+------------------------------------
   Reporter:  [EMAIL PROTECTED]  |                Owner:  adrian                
     Status:  closed                   |            Component:  Admin interface 
      
    Version:                           |           Resolution:  invalid         
      
   Keywords:                           |                Stage:  Design decision 
needed
  Has_patch:  0                        |           Needs_docs:  0               
      
Needs_tests:  0                        |   Needs_better_patch:  0               
      
---------------------------------------+------------------------------------
Comment (by anonymous):

 Since the answer here wasn't immediately clear to me (I just started using
 Django today, so forgive me if I'm a bit dense!)
 
 You can use the admin search fields to allow you to search on a related
 model by using a double underscore {{{__}}} something like
 {{{localFKfield__relatedfield}}} would be the generic form I guess
 
 Quick Example
 
 
 {{{
 class Partner(models.Model):
         name = models.CharField('Name of Partner
 Org',maxlength=255,unique=True)
         website = models.URLField('Website of Partner Org', blank=True)
 
         def __str__(self):
                 return self.name
 
         class Admin:
                 pass
 
         class Meta:
                 verbose_name = "Partner Organization"
                 verbose_name_plural = "Partner Organizations"
 class Country(models.Model):
         name = models.CharField('Name of Country',maxlength=255)
         iso_code = models.IntegerField('ISO code')
         subregion = models.CharField('Sub-region',maxlength=255)
         subregion_iso = models.IntegerField('ISO code of Sub-region')
         parentregion = models.CharField('Parent-region',maxlength=255)
         parentregion_iso = models.IntegerField('ISO code of Parent-
 region')
 
         def __str__(self):
                 return self.name
 
         class Admin:
                 pass
 
         class Meta:
                 verbose_name = "Country"
                 verbose_name_plural = "Countries"
 
 class Request(models.Model):
         request_id = models.CharField('Request ID
 ###N/AYY',primary_key=True,maxlength=6,unique=True)
         req_status = models.ForeignKey(ReqStatus, verbose_name = 'Current
 Status')
         partner_id = models.ForeignKey(Partner, verbose_name = 'Primary
 Partner')
         country_iso = models.ForeignKey(Country, verbose_name = 'Primary
 Destination Country')
         request_date = models.DateField('Date request received')
         request_boxes = models.IntegerField('Number of boxes requested')
         request_desc = models.TextField('Long description of request')
         contact_name = models.CharField('Name of Primary Contact at
 Partner Org.',maxlength=255)
         contact_email = models.EmailField("Contact's Email", blank=True)
         contact_phone = models.PhoneNumberField("Contact's Phone
 #",blank=True)
         fmsc_rep = models.CharField('Name of FMSC
 Representative',maxlength=255)
         reply_date = models.DateField('Date on which FMSC first replied')
         comments = models.TextField('Comments', blank=True)
 
         def __str__(self):
                 return self.request_id+" - "+str(self.country_iso)+" via
 "+str(self.partner_id)+" ("+str(self.request_boxes)+" boxes)"
 
         class Admin:
                 list_filter = ['req_status']
                 search_fields =
 ['request_id','partner_id__name','country_iso__name']
                 pass
 
         class Meta:
                 verbose_name = "Request"
                 verbose_name_plural = "Requests"
 
 }}}
 
 So the line {{{search_fields = ['request_id', 'partner_id__name',
 'country_iso__name']}}} means that you can now look up requests based on
 the request_id, the name of the partner OR the name of the country

-- 
Ticket URL: <http://code.djangoproject.net/ticket/3345#comment:8>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" 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-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to