-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,16 @@ | ||
How to filter FK dropdown values in django admin? | ||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
Your :code:`Hero` model has a FK to :code:`Category`. | ||
So all category objects will show in the admin dropdown for category. If instead, you wanted to see only a subset, | ||
Django allows you to customize that by overriding :code:`formfield_for_foreignkey`:: | ||
|
||
@admin.register(Hero) | ||
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin): | ||
... | ||
def formfield_for_foreignkey(self, db_field, request, **kwargs): | ||
if db_field.name == "category": | ||
kwargs["queryset"] = Category.objects.filter(name__in=['God', 'Demi God']) | ||
return super().formfield_for_foreignkey(db_field, request, **kwargs) | ||
|
||
.. image:: filter_fk_dropdown.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,26 @@ | ||
How to manage a model with a FK with a large number of objects? | ||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
You can create a large number of categories like this:: | ||
|
||
categories = [Category(**{"name": "cat-{}".format(i)}) for i in range(100000)] | ||
Category.objects.bulk_create(categories) | ||
|
||
|
||
Now as :code:`Category` has more than 100000 objects, when you go to the :code:`Hero` admin, it will have category dropdown with 100000 selections. | ||
This will make the page both slow and the dropdown hard to use. | ||
|
||
You can change how admin hand;es it by setting the :code:`raw_id_fields`:: | ||
|
||
@admin.register(Hero) | ||
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin): | ||
... | ||
raw_id_fields = ["category"] | ||
|
||
This change the Hero admin to look like: | ||
|
||
.. image:: raw_id_fields_1.png | ||
|
||
Add the popup looks like this | ||
|
||
.. image:: raw_id_fields_2.png |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,31 @@ | ||
How to create a single Django admin from two different models? | ||
================================================================= | ||
|
||
|
||
:code:`Hero` has a FK to :code:`Category`, so you can select a category from Hero admin. | ||
If you want to also be able to create :code:`Category` objects from Hero admin, you can change the form for Hero admin, and customise the | ||
:code:`save_model` behaviour.:: | ||
|
||
class HeroForm(forms.ModelForm): | ||
category_name = forms.CharField() | ||
|
||
class Meta: | ||
model = Hero | ||
exclude = ["category"] | ||
|
||
|
||
@admin.register(Hero) | ||
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin): | ||
form = HeroForm | ||
.... | ||
|
||
def save_model(self, request, obj, form, change): | ||
category_name = form.cleaned_data["category_name"] | ||
category, _ = Category.objects.get_or_create(name=category_name) | ||
obj.category = category | ||
super().save_model(request, obj, form, change) | ||
|
||
|
||
With this change, your admin looks like below and has allows creating or updating category from the Hero admin. | ||
|
||
.. image:: single_admin_multiple_model.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters