Skip to content

Commit

Permalink
Added edit mutiple models chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 21, 2018
1 parent 956a8e3 commit 11cab61
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/changeview_readonly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ You can do this by::

Your create form looks like this:

.. image::changeview_readonly.png
.. image:: changeview_readonly.png
Binary file added docs/edit_multiple_models.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions docs/edit_multiple_models.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
How to edit mutiple models from one Django admin?
=====================================================

To be able to edit multiple objects from one Django admin, you need to use inlines.

You have the :code:`Category` model, and you need to add and edit :code:`Villain` models inside the admin for Category. You can do::


class VillainInline(admin.StackedInline):
model = Villain

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
..

inlines = [VillainInline]

You can see the form to add and edit :code:`Villain` inside the :code:`Category` admin. If the Inline model has alot of fields,
use :code:`StackedInline` else use :code:`TabularInline`.



.. image:: edit_multiple_models.png


6 changes: 6 additions & 0 deletions heroes_and_monsters/entities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,16 @@ class VillainAdmin(admin.ModelAdmin, ExportCsvMixin):

readonly_fields = ["added_on"]


class VillainInline(admin.StackedInline):
model = Villain

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ("name",)

inlines = [VillainInline]

def has_add_permission(self, request):
return False

Expand Down
10 changes: 10 additions & 0 deletions heroes_and_monsters/entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ class Villain(Entity):
is_unique = models.BooleanField(default=True)
count = models.PositiveSmallIntegerField(default=1)


class HeroAcquaintance(models.Model):
"Non family contacts of a Hero"
hero = models.OneToOneField(Hero, on_delete=models.CASCADE)

friends = models.ManyToManyField(Hero, related_name="+")
detractors = models.ManyToManyField(Hero, related_name="+")
main_anatagonists = models.ManyToManyField(Villain, related_name="+")


class AllEntity(models.Model):
name = models.CharField(max_length=100)

Expand Down

0 comments on commit 11cab61

Please sign in to comment.