Django admin exclude filter - django

I'm looking for a way to implement exclude filter in django admin list view.
The case is the following:
In django admin I have a list of model instances, and I need to be able to show only instances that does not belong to some user for example (the user is the FK in the described model)
Is there any solution for this case?

You can always make your own filter. See an example here.

Related

How to edit the Django Admin User social auths listing page?

I am having trouble finding where this admin file exists so I can add an extra field. I think it's auto-magically created upon setup.
I want to add a date field, specifically, to the listing page (shown below), perhaps after the UID field so I can know when the user auth was created.
screenshot of django user social auths listing page
Okay here's what I've tried using Django-allauth and I think it somehow works the same with django-socialauth. Just get the gist of the idea and work it to your code
Extend first the SocialAccountAdmin in any of your admin.py files, better if in a specific app like "user", "home", or whatever you prefer.
admin.py
from allauth.socialaccount.admin import SocialAccountAdmin
from allauth.socialaccount.models import SocialAccount
class MySocialAccount(SocialAccountAdmin):
list_display = ('user', 'uid', 'provider', 'date_joined') # I haven't tried just adding a certain list to the list_display, for the meantime add all necessary fields just like how socialauth did
admin.site.unregister(SocialAccount) # Need to unregister the default socialaccount admin
admin.site.register(SocialAccount, MySocialAccount) # Then register it back with the custom made admin
There may be perhaps a better way to do this but this did the work.
Can it be interesting to just add a field to your model ? Adding a DateField for your creation date. Probably you need to understand learn more with : https://docs.djangoproject.com/en/3.0/ref/models/fields/

Django admin foreign key field filtering

I have client model which has a foreign key field to a country model.
So in Django admin, when I create a client and I select the country where this client belongs to. but the problem is the select list is too long(too many countries on this planet). Sometimes it takes just too long to get the one I need.
So I wonder if there is other widget in djano admin that provides a select-input-combo widget.
When I type in the input and it will filter out the right one for me to select.
Hope you can understand what I need here.
did you see raw_id_fields ?
you would do in admin.py something like:
class ClientAdmin(admin.ModelAdmin):
raw_id_fields = ("country",)
admin.site.register(Client, ClientAdmin)
then select widget will become something like:
Since Django 2.0 there is autocomplete_fields. From the documentation:
autocomplete_fields is a list of ForeignKey and/or ManyToManyField fields you would like to change to Select2 autocomplete inputs.
and
The Select2 input looks similar to the default input but comes with a search feature that loads the options asynchronously. This is faster and more user-friendly if the related model has many instances.
Note that you need to define search_fields in the related object's ModelAdmin since it is used by the widget.

Force relationships on django models for models that aren't officially FK related

I'm trying to figure out how to get the django admin system to display my models as inlines, when there isn't a direct FK from child to parent model.
I have three models (pseudo code):
class CampaignMain(models.model):
...
class CampaignMonitor(models.model):
campaign = models.OneToOneField(CampaignMain, pk=True)
class CampaignTransaction(models.model):
campaign = models.ForeignKey(CampaignMain)
So both CampaignMonitor and CampaignTransaction FK CampaignMain, which is the way I need it to be structured.
Here's the bit I can't fathom: I need an admin page showing CampaignMonitor with CampaignTransaction as inlines. But when I try this, I get "error no fk in CampaignTransaction pointing to CampaignMonitor"
Is there a way to "force" the relationship just for the admin page? Or is there a generic FK option? I saw something in contrib/contenttypes, but it doesn't seem to be what I need. Or am I going to have to build a custom admin section to two models in that way?
As always advice is greatly appreciated.
imanc
Instead of OneToOneField you can use Multi-table inheritance, which implemented using a one-to-one relationshinp:
class CampaignMonitor(CampaignMain):
...
Now modify CampaignMonitor's admin as needed for your needs.

How to modify the way a ForeignKey field is rendered in a Django admin page to avoid browser crash?

I have a Customer model which contains a ForeignKey to a Contact model.
I have over 100,000 contacts in my DB and when I load the admin page for a specific customer, the dropdown menu for the contact is getting populated with ALL of the contacts in the database. This has recently, due to its shear length, started causing my Firefox to crash while the admin page is loading.
Is there a way to either:
replace the field with an integer
field I can manually modify to the
contact ID when necessary
replace the dropdown menu with some
alternative input method which won't
crash the browser
remove this input
from the Customer admin page
altogether
Thanks!
You can do any of the either of things you want to.
Simplest solution is the exclude the field from the admin. Just say so in the admin class.
You can change the field to be text input and display it's primary key rather than the item itself, by including it in the raw_id_fields of the admin class.
You can also replace the standard dropdown widget with the Auto complete text field input. Use the implemented widget, or other equivalents. - This is probably the solution you like the best.
You can also override the formfield_for_foreignkey method on the Admin model to customize the queryset that gets displayed in the foreign-key dropdown. You may want to checkout my implementation for displaying only the current User's (or subdomain's) added entities.
Sounds like specifying the contact field in raw_id_fields in your admin.py entry for the relevant model would sort you out. Docs are here.
PS. Surprised (but not that surprised) that FF gives out before your database server tanks...

Does Django admin provide group edit of models?

For instance, I have a model called Person, and it has a bool field called 'isAthlete'. I would like to be able to check off True for 50 of these Person records, and then hit submit, without having to go into each Person model record and make the change. Is there an easy or already provided way to set this up in Django?
you can do this using django admin actions, http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/