I'm trying to create full text search on model, everything goes fine
when
searching TextFields but I have a problem with ForeignKey field.
How can i do that? Can anyone point me to the right direction?
Thanks
Model example:
class Model1(models.Model):
text_field =models.TextField(max_length=250)
fk_field = models.ForeignKey('Model2')
class Model2(models.Model):
text_field = models.TextField(max_length=250)
Thanks
R.
I would like to see your sphinx.conf.
Well for this which creating your indexer in sphinx.conf you need to specify what fields you select. After that what fields from your sql you select to be put as attributes (i.e. non searchable fields).
I suppose search with foreign-key fields are just as normal search. What particular difficulty/error are you facing?
Related
I have a model:
class Rent(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
bikes = models.ManyToManyField(Bike)
when I create a new rent in the admin site, I would like to exclude the bikes that have the value of 1 for their status integer field from showing in the list to choose from. Is this possible?
Thanks
I am not sure if you can do that from the Admin site. probably you cannot. But you can do it from views. just select the model objects you need with query and pass it into the field. I am attaching a ell explained similar scenario. condition your query and add those objects.
Adding many to many fields based on condition
You can customize the admin form. Here is a discussion of how to filter your form choices.
Thank you all! I figured it out, the problem was that the limit_choices_to doesn't work when I change the filter to inline and I found others with the same problem, so I just changed it to filter_horizontal and now it works as it should
How to tell django-tables2 which columns I would like to have in table? I know there is this Column attribute 'visible', which can be set to False.
However I have a model with many fields, and would like to display just some of them, so writing a complete list of all columns, just to tell that most of them will not be visible, does not seem the right approach.
What I am looking for is a way to provide list of column names to be displayed, if this is possible then maybe even give user the ability to select which columns he wants.
The other solution came to my mind - make that 'visible' attribute False by default, but since it is defined in Column class, I would still need to write a complete list.
Since I have not found any django-tables2 discussion forum, I ask here.
Example of specifying model fields
Your Model
class Product(model.Models):
name = model.CharField(max_length=20)
price = model.DecimalField(max_digit=9, decimal_places=2)
Your Table
class ProductTable(tables.Table):
actions = ProductActions(orderable=False) # custom tables.Column()
class Meta:
model = Product
fields = ('name', 'price', 'action') # fields to display
Also you can also use exclude
Related docs entry here
I apologize if this question has already been answered, but I couldn't find it anywhere else.
In my django project, I have foreignkey fields which have hundreads of options for selection.
The django dropdown default widget for foreignkey fields makes it really difficult to find the one I want.
I have crossed with the filter_horizontal admin option, which brings great selection functionality for many to many fields with a textual search field and two selectors.
I was wondering if there is a django built-in option or if any of you has found a solution that allows me do a textual search "on-the-fly" for foreignkey fields as in many to many fields specified in "filter_horizontal"
Thanks in advance.
Check out raw_id_fields
Old question, but if anyone lands here: Today we can use autocomplete_fields like this:
class MyRelatedClassAdmin(admin.ModelAdmin):
search_fields = ['name']
class MyClassAdmin(admin.ModelAdmin):
autocomplete_fields = ('related', )
admin.site.register(MyClass, MyClassAdmin)
admin.site.register(MyRelatedClass, MyRelatedClassAdmin)
where MyClass has a ForeignKey-field named related. The name field of the related model will be used.
I have a reasonably complex custom Django model method. It's visible in the admin interface, and I would now like to make it sortable in the admin interface too.
I've added admin_order_field as recommended in this previous question, but I don't fully understand what else I need to do.
class Book(models.Model):
id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=200)
library_id = models.CharField(max_length=200, unique=True)
def current_owner(self):
latest_transaction = Transaction.objects.filter(book=self)[:1]
if latest_transaction:
if latest_transaction[0].transaction_type==0:
return latest_transaction[0].user.windows_id
return None
current_owner.admin_order_field = 'current_owner'
Currently, when I click on the current_owner field in the admin interface, Django gives me
FieldError at /admin/books/book/
Cannot resolve keyword 'current_owner' into field
Do I need to make a BookManager too? If so, what code should I use? This isn't a simple Count like the example in the previous question, so help would be appreciated :)
Thanks!
The Django admin won't order models by the result of a method or any other property that isn't a model field (i.e. a database column). The ordering must be done in the database query, to keep things simple and efficient.
The purpose of admin_order_field is to equate the ordering of a non-field property to the ordering of something that is a field.
For example, a valid values current_owner.admin_order_field could be id, title or library_id. Obviously none of these makes sense for your purpose.
One solution would be to denormalise and always store current_owner as a model field on Book; this could be done automatically using a signal.
You can't do this. admin_order_field has to be a field, not a method - it's meant for when you have a method that returns a custom representation of an underlying field, not when you do dynamic calculations to provide the value. Django's admin uses the ORM for sorting, and that can't sort on custom methods.
How do I travel through multiple foreign keys in Django? I've tried everything I can think of from the django docs, but I'm obviously missed something (extreme newbie). I have models for scientists, experiments, and theories.
If I want to look at a particular Theory (let's call it 'relativity') and get a list of all of the emails of scientists working on it (kept in the normal django user model), how do I do this?
class Experiment(models.Model)
experimenter = models.ForeignKey(Scientist)
theory = models.ForeignKey(Theory)
class Theory(models.Model)
name = models.CharField(max_length=100)
class Scientist(models.Model)
user = models.ForeignKey(User, unique=True)
institution = models.CharField(max_length=20, null=True, blank=True)
These are simplified versions of my models that I rewrote, so there are probably some errors in it, but the relationships are correct.
I've tried every kind of combinations of select_related(), get(), filter() but can't figure it out. Thanks in advance for your help!
User.objects.filter(scientist__experiment__theory__name=u'relativity')
Take a look at the Django documentation section about Lookups that span relationships. The net takeaway is:
To span a relationship, just use the field name of related fields across models, separated by double underscores, until you get to the field you want.
Ignacio's answer shows an example of using the double underscores on field names to span a relationship.
The other relevant portion of Django's documentation would be the Related objects section. Relationships in Django are asymmetrical in the way they are accessed. Forward/normal relationships are accessed as attributes of the models. Backward relationships are accessed:
Django also creates API accessors for the "other" side of the relationship -- the link from the related model to the model that defines the relationship. For example, a Blog object b has access to a list of all related Entry objects via the entry_set attribute: b.entry_set.all().