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.
Related
Assume I have a model named MyModel and I have a Field Named field Now I want to add three more fields inside the prescription like one field_a , field_b and field_c .
Does Django Allow that in any way or we have to make another model for this purpose and then link with Foreign Key to MyModel?
Well I think it is an idea that could lead to some really hard to maintain code and should be well thought through.
That aside If you have a look at:
https://docs.wagtail.io/en/stable/topics/streamfield.html
It's a special field meant for cms applications that uses json in the field to model dynamic fields and validations for it.
Also if you run postgres this might help. https://docs.djangoproject.com/en/3.2/ref/contrib/postgres/fields/#jsonfield
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
I am building a blog site and have models in respect of Category and Posts. Posts have a Many to Many relationship for Category.
class Post(models.Model):
categories = models.ManyToManyField(Category)
Everything is working fine aside from the fact that in the Category list in the template I only want to load categories that actually have posts.
If a category is empty I don't want to display it, I have tried to define a relationship in Category to Post to allow me to use something like {{ if category.posts }}. At the moment using another Many to Many field in Category is presently giving me an extra field in admin which I don't really want or feel that's needed.
How is best to navigate this relationship, or create one that's suitable?
Cheers
Kev
Django automatically creates a field on the related model of any ForeignKey or ManyToMany relationship. You can control the name of the field on the related model via the related_name option like that:
class Post(models.Model):
categories = models.ManyToManyField(Category,related_name='posts')
This way, your approach works without any additional fields. Btw, if you leave out the related_name argument, Django will create by default one with [field_name]_set.
You can use reverse relations on ManyToMany fields. In the reverse filter, you must use the related model name (if you did not use related_name attribute). So in your question you can use model name as the reverse name like:
{% if category.post %}
You can also use this in your filtering functions in views:
Category.objects.filter(post__isnull=False)
Reverse relation name must be lowercase.
Check the documentation here
I have a Django model and I wish to denote that some of the model fields are private so that when I display a ModelForm based on this model I can display these fields marked as such.
I'd like this to be specified on the model rather than the form as I think that's where it belongs.
I'm wondering what the best way to do this is. Can I write a decorator #private to do this? Can anyone point me at an example?
Thanks
There's no one feature that perfectly fits your problem, but here's a couple of suggestions:
Add the information to the model's Meta class, which you can then access from your form via the _meta attribute on the model
Utilise the help_text option on your model fields (obviously this won't help you to "decide programatically which fields I should be displaying to other users")
I do agree that it's debatable as to whether this belongs at the model layer; it seems like business logic to me.
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?