-
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
4 changed files
with
153 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,27 @@ | ||
How to add One to One relation as admin inline? | ||
================================================ | ||
|
||
OneToOneFields can be set as inlines in the same way as a FK. However, only one side of the OneToOneField can be set as the inline model. | ||
|
||
You have a :code:`HeroAcquaintance` model which has a One to one relation to hero like this.:: | ||
|
||
|
||
|
||
class HeroAcquaintance(models.Model): | ||
"Non family contacts of a Hero" | ||
hero = models.OneToOneField(Hero, on_delete=models.CASCADE) | ||
.... | ||
|
||
You can add this as inline to Hero like this:: | ||
|
||
class HeroAcquaintanceInline(admin.TabularInline): | ||
model = HeroAcquaintance | ||
|
||
|
||
@admin.register(Hero) | ||
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin): | ||
... | ||
inlines = [HeroAcquaintanceInline] | ||
|
||
|
||
.. image:: one_to_one_inline.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
101 changes: 101 additions & 0 deletions
101
heroes_and_monsters/entities/migrations/0002_auto_20180221_1830.py
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Generated by Django 2.0 on 2018-02-21 18:30 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('entities', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='AllEntity', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=100)), | ||
], | ||
options={ | ||
'db_table': 'entities_entity', | ||
'managed': False, | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='HeroAcquaintance', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='HeroProxy', | ||
fields=[ | ||
], | ||
options={ | ||
'proxy': True, | ||
'indexes': [], | ||
}, | ||
bases=('entities.hero',), | ||
), | ||
migrations.AlterModelOptions( | ||
name='category', | ||
options={'verbose_name_plural': 'Categories'}, | ||
), | ||
migrations.AlterModelOptions( | ||
name='hero', | ||
options={'verbose_name_plural': 'Heroes'}, | ||
), | ||
migrations.AddField( | ||
model_name='hero', | ||
name='added_by', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AddField( | ||
model_name='hero', | ||
name='added_on', | ||
field=models.DateField(auto_now=True), | ||
), | ||
migrations.AddField( | ||
model_name='hero', | ||
name='headshot', | ||
field=models.ImageField(blank=True, null=True, upload_to='hero_headshots/'), | ||
), | ||
migrations.AddField( | ||
model_name='villain', | ||
name='added_by', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AddField( | ||
model_name='villain', | ||
name='added_on', | ||
field=models.DateField(auto_now=True), | ||
), | ||
migrations.AlterField( | ||
model_name='hero', | ||
name='father', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='entities.Hero'), | ||
), | ||
migrations.AddField( | ||
model_name='heroacquaintance', | ||
name='detractors', | ||
field=models.ManyToManyField(related_name='_heroacquaintance_detractors_+', to='entities.Hero'), | ||
), | ||
migrations.AddField( | ||
model_name='heroacquaintance', | ||
name='friends', | ||
field=models.ManyToManyField(related_name='_heroacquaintance_friends_+', to='entities.Hero'), | ||
), | ||
migrations.AddField( | ||
model_name='heroacquaintance', | ||
name='hero', | ||
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='entities.Hero'), | ||
), | ||
migrations.AddField( | ||
model_name='heroacquaintance', | ||
name='main_anatagonists', | ||
field=models.ManyToManyField(related_name='_heroacquaintance_main_anatagonists_+', to='entities.Villain'), | ||
), | ||
] |
20 changes: 20 additions & 0 deletions
20
heroes_and_monsters/templates/entities/heroes_changelist.html
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% extends 'admin/change_list.html' %} | ||
|
||
|
||
{% block object-tools %} | ||
<div> | ||
<form action="immortal/" method="POST"> | ||
{% csrf_token %} | ||
<button type="submit">Make Immortal</button> | ||
</form> | ||
<form action="mortal/" method="POST"> | ||
{% csrf_token %} | ||
<button type="submit">Make Mortal</button> | ||
</form> | ||
</div> | ||
<br /> | ||
{{ block.super }} | ||
|
||
|
||
|
||
{% endblock %} |