I'm trying to make only the 1st inline object boolean field True
by default only for Add page.
So, I have Person
model and Email
model which has the foreign key of Person
model as shown below:
# "models.py"
class Person(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
class Email(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
is_used = models.BooleanField()
email = models.EmailField()
def __str__(self):
return self.email
Then, I set the custom form EmailForm
to form
in EmailInline()
as shown below:
# "admin.py"
class EmailForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
kwargs['initial'] = {'is_used': True}
super().__init__(*args, **kwargs)
class EmailInline(admin.TabularInline):
model = Email
form = EmailForm # Here
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
inlines = (EmailInline,)
But, all inline objects are is_used=True
by default on Add page as shown below:
And, I added one main object with one inline object which is is_used=True
as shown below:
But, the added inline object and other not-yet-added inline objects are is_used=True
on Change page as shown below:
And, I added one main object with one inline object which is is_used=False
as shown below:
But, the added inline object and other not-yet-added inline objects are is_used=True
on Change page as shown below:
So, how can I make only the 1st inline object boolean field True
by default only for Add page?