I’ve two models Person with many to many on itself and Relationship as a through for that field.
How to add additional form/forms to formset of that many-to-many relation dynamically?
models.py:
class Person(models.Model):
first_name = models.CharField(_('first name'), max_length=30, blank=True, null=True) last_name = models.CharField(_('last name'), max_length=30, blank=True, null=True) related_to_me = models.ManyToManyField( 'self', null=True, blank=True, related_name='me_related_to', symmetrical=False, through=Relationship)
class Relationship(models.Model):
class TypeChices(models.TextChoices): PARENT = 'PA', _('is my Parent') SPOUSE = 'SP', _('is my Spouse') person = models.ForeignKey('Person', on_delete=models.CASCADE, related_name='related_person') related_person = models.ForeignKey('Person', on_delete=models.CASCADE, related_name='person') relation_type = models.CharField(max_length=2, choices=TypeChices, default=TypeChices.PARENT)
admin.py
class RelationshipInline(admin.TabularInline):
model = Relationship
extra = 1
fk_name = ‘person’
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):inlines = [RelationshipInline]
In the admin site i can see all person spouses or parents, but I want to add children as well. How to prepopulate the corresponding formset with the data?