|
Project Information
|
AutoComplete for ForeignKey and ManyToManyFielduses same syntax as the search for django-admin up: the latest version in Downloads
requirementscopy to the appropriate folder
example
use a fairly simplemodels.pyin m2m field need to specify related_name class Celebrity(models.Model): name = models.CharField() class Film(models.Model): type = models.ForeignKey( Type ) director= models.ManyToManyField( Celebrity, related_name="director") actor = models.ManyToManyField( Celebrity, related_name="actor") NEW: for inline class Ceremony( PageSeo ): name =models.CharField( max_length = 200 ) guiding =models.ManyToManyField( Celebrity, related_name = "guiding", blank = True ) best_film=models.ForeignKey( Film, default = None, null = True, blank = True ) class Nominee( models.Model ): ceremony =models.ForeignKey( Ceremony ) film =models.ForeignKey( Film, blank = True, null = True, on_delete = models.SET_NULL ) celebrity=models.ForeignKey( Celebrity, blank = True, null = True, on_delete = models.SET_NULL ) admin.pyfrom apps.autocomplete.widgets import *
class FilmAdmin(AutocompleteModelAdmin):
related_search_fields = {
'type': ('title',),
'actor': ('^name',),
'director': ('^name',),
}
admin.site.register( Film, FilmAdmin )NEW: for inline class NomineeInline( AutocompleteTabularInline ):
model=Nominee
extra=0
""" describe the model fields for the search is only used for rendering the widget """
related_search_fields={
'film': ( 'name', ),
'celebrity': ( 'name', ),
}
class CeremonyAdmin( AutocompleteModelAdmin ):
inlines=[
NomineeInline,
]
related_search_fields={
""" describe the model fields for the search """
'guiding': ( 'name', ),
'best_film': ( 'name', ),
""" re-describes the same field as in inline is used to search for """
'film': ( 'name', ),
'celebrity': ( 'name', ),
}"related_search_fields" parameter is used to specify on what fields you want to search 'actor' and 'director' ties are the names given in "related_name" query syntax is similar to searching in admin panel http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields I use the " 'actor': ('^name',) " operator ^ means the beginning of the field. and eventually will be formed about the substitution request form For example, if related_search_fieldsis set to ('^name',') and a user searches for john lennon, Django will do the equivalent of this SQL WHERE clause: WHERE name ILIKE 'john%' AND name ILIKE 'lennon%' NEWYou can also use the admin form and the additional fields are not described in the model which will refer to any existing model by WildModelSearchInput widget search_fields format - ( "search_field", "result_field") class FilmAdminForm( forms.ModelForm ):
class Meta:
model=Film
discussion = forms.CharField( required=False, widget=WildModelSearchInput(app_label='forum',model_name='Topic', search_fields=('name','id')))when saving in the discussion will be chosen topic id and you can handle it on your own |