Comments (16)
Having a `hit.model' object one could use generic Python introspection:
from <project>.<application>.models import MyModel, MyAnotherModel
...
indexers = [MyModel.indexer, MyAnotherModel.indexer]
indexer CompositeIndexer(*indexers)
for hit in indexer.search("search terms"):
if isinstance(hit.model, MyModel):
# do something
...
There's also Django's Options class instance `hit.model._meta` which has
`app_label` and `db_table` attributes among others (see
http://docs.djangoproject.com/en/dev/ref/models/options/#available-meta-options)
Original comment by [email protected]
on 21 Jun 2010 at 3:09
from djapian.
Original comment by daevaorn
on 21 Jun 2010 at 3:43
- Added labels: Type-Enhancement
- Removed labels: Type-Defect
from djapian.
You're not allowed to access _ attributes in the template.
And looping through everything twice doesn't make sense.
Now I'm searching users and forum posts. The results should link to totally
different things. get_absolute_url is nice but kinda evil and unusable for the
forum posts.
so I just want to be able to check the app_name and model_name without having
to write a whole templatetag for it. Either add them as properties to the hit
class or add a template tag to get them from a hit.
Original comment by [email protected]
on 22 Jun 2010 at 11:56
from djapian.
I think we add `model_name` attribute to hit object.`````
Original comment by daevaorn
on 22 Jun 2010 at 12:36
- Changed state: Accepted
- Added labels: Milestone-Release2.5
from djapian.
I'll be needing this pretty soon for a big project. If it's not in by then I'll
probably write a patch myself. If I do I'll post it here.
Original comment by [email protected]
on 23 Jun 2010 at 8:49
from djapian.
I would suggest to go with a templatetag.
How do you usually get a `model_name` for a standard Django QuerySet results
item?
Original comment by [email protected]
on 24 Jun 2010 at 7:18
from djapian.
What I did now to quickly overcome the problem is this:
@register.filter
def get_model_from_hit(hit):
"""
Returns the `app_label.model_name` for the hit
"""
content_type = ContentType.objects.get_for_model(hit.model)
return "%s.%s" % (content_type.app_label, content_type.model)
Original comment by [email protected]
on 24 Jun 2010 at 7:29
from djapian.
Having a filter is exactly the stadard way how to get Django model's options ;)
I would not use ContentType here as it will make addition hit into the database.
Please consider the code below.
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def get_model_from_hit(hit):
"""
Returns the `app_label.model_name` for the hit
"""
opts = hit.model._meta
return "%s.%s" % (opts.app_label, opts.module_name) # there is also opts.object_name which is the exact model's class name like `MyCoolModel`
get_model_from_hit.is_safe = True
Original comment by [email protected]
on 24 Jun 2010 at 7:45
from djapian.
ContentTypes are cached.. as long as the server is running it hits the database
only once ;-)
But yeah.. your method would be better :)
Original comment by [email protected]
on 24 Jun 2010 at 7:57
from djapian.
Oh.. and you don't want the string filter thing. It converts all the input to
unicode, which the hit isn't :)
Original comment by [email protected]
on 24 Jun 2010 at 7:59
from djapian.
Yep, @stringfilter is not need here - I copy-pasted the decorators from another
filter ;)
Original comment by [email protected]
on 24 Jun 2010 at 8:02
from djapian.
So the final version is:
from django import template
register = template.Library()
@register.filter
def get_model_from_hit(hit):
"""
Returns the `app_label.model_name` for the hit
"""
opts = hit.model._meta
return "%s.%s" % (opts.app_label, opts.module_name)
get_model_from_hit.is_safe = True
Original comment by [email protected]
on 24 Jun 2010 at 8:13
from djapian.
I think this filter can be more generalized (accepts model itself not `hit`
instance) and ships separately.
Close ticket?
Original comment by daevaorn
on 24 Jun 2010 at 12:54
from djapian.
I'd vote for not making this a part of Djapian, and closing the issue as
"WontFix"
Original comment by [email protected]
on 25 Jun 2010 at 5:30
from djapian.
I think it should be part of djapian. You will need this in every
CompositeIndex template because you otherwise don't know where to link the
result to.
Original comment by [email protected]
on 25 Jun 2010 at 6:38
from djapian.
The usage of search results is an application domain. The filter above is just
an example. Having a "app_label.model_name" string wouldn't help a lot for
"linking". You could use {{{hit.instance.get_get_absolute_url}}} in your
template to get a public link to the result hit instance whatever "type" it has.
Original comment by [email protected]
on 30 Jun 2010 at 6:25
from djapian.
Related Issues (20)
- Xapian 1.2 support HOT 8
- Running manage.py index causes Exception HOT 2
- Add a recursive field resolver
- Indexer fails with null values HOT 3
- Incompatibility with latest Xapian 1.2.2 release HOT 4
- A management command for per-application index rebuild HOT 4
- have to constantly rebuild index HOT 12
- AssertionError on djapian.tests.query._QueryTest_subject:test and djapian.tests.query._QueryTest_title:test HOT 7
- Error on prefetch when objects is already deleted, but xapian index not updated, yet HOT 6
- CompositeIndexer not returning tags HOT 1
- Incorrect ordering HOT 2
- CompositeIndexer does not support collapsing by a [common] tag. HOT 2
- Store Arbitrary data in search db HOT 4
- order_by() does not change results ordering
- manage.py index fails on deletion of a model with multiple indexers HOT 1
- Add documentation about install xapian on macOSX via homebrew
- xapian.DatabaseModifiedError exception is unhadled HOT 6
- ./manage.py index does not work in django 1.4 HOT 5
- Tag searches on composite indexes is not working properly
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from djapian.