diff --git a/people/admin.py b/people/admin.py index 6d3f263..94119a7 100644 --- a/people/admin.py +++ b/people/admin.py @@ -16,16 +16,22 @@ class PersonAdmin(admin.ModelAdmin): pass -@admin.register(models.RelationshipQuestion) -class RelationshipQuestionAdmin(admin.ModelAdmin): - pass - - @admin.register(models.RelationshipQuestionChoice) class RelationshipQuestionChoiceAdmin(admin.ModelAdmin): pass +class RelationshipQuestionChoiceInline(admin.TabularInline): + model = models.RelationshipQuestionChoice + + +@admin.register(models.RelationshipQuestion) +class RelationshipQuestionAdmin(admin.ModelAdmin): + inlines = [ + RelationshipQuestionChoiceInline, + ] + + @admin.register(models.Relationship) class RelationshipAdmin(admin.ModelAdmin): pass diff --git a/people/migrations/0006_relationship_questions_order.py b/people/migrations/0006_relationship_questions_order.py new file mode 100644 index 0000000..2dc7495 --- /dev/null +++ b/people/migrations/0006_relationship_questions_order.py @@ -0,0 +1,31 @@ +# Generated by Django 2.2.10 on 2020-02-20 10:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('people', '0005_user_one_person'), + ] + + operations = [ + migrations.AlterModelOptions( + name='relationshipquestion', + options={'ordering': ['order', 'text']}, + ), + migrations.AlterModelOptions( + name='relationshipquestionchoice', + options={'ordering': ['order', 'text']}, + ), + migrations.AddField( + model_name='relationshipquestion', + name='order', + field=models.SmallIntegerField(default=0), + ), + migrations.AddField( + model_name='relationshipquestionchoice', + name='order', + field=models.SmallIntegerField(default=0), + ), + ] diff --git a/people/models.py b/people/models.py index c49e44c..180c8b0 100644 --- a/people/models.py +++ b/people/models.py @@ -1,3 +1,5 @@ +import typing + from django.conf import settings from django.contrib.auth.models import AbstractUser from django.db import models @@ -54,6 +56,12 @@ class RelationshipQuestion(models.Model): """ Question which may be asked about a relationship. """ + class Meta: + ordering = [ + 'order', + 'text', + ] + #: Version number of this question - to allow modification without invalidating existing data version = models.PositiveSmallIntegerField(default=1, blank=False, null=False) @@ -62,6 +70,19 @@ class RelationshipQuestion(models.Model): text = models.CharField(max_length=1023, blank=False, null=False) + #: Position of this question in the list + order = models.SmallIntegerField(default=0, + blank=False, null=False) + + @property + def choices(self) -> typing.List[typing.List[str]]: + """ + Convert the :class:`RelationshipQuestionChoices` for this question into Django choices. + """ + return [ + [choice.pk, str(choice)] for choice in self.answers.all() + ] + def __str__(self) -> str: return self.text @@ -75,6 +96,10 @@ class RelationshipQuestionChoice(models.Model): models.UniqueConstraint(fields=['question', 'text'], name='unique_question_answer') ] + ordering = [ + 'order', + 'text', + ] #: Question to which this answer belongs question = models.ForeignKey(RelationshipQuestion, related_name='answers', @@ -85,6 +110,10 @@ class RelationshipQuestionChoice(models.Model): text = models.CharField(max_length=1023, blank=False, null=False) + #: Position of this answer in the list + order = models.SmallIntegerField(default=0, + blank=False, null=False) + def __str__(self) -> str: return self.text