What I'm trying to do is assert the start_date
of the child form is after the start_date
of the parent form.
For example, if I have the following models:
class Parent(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
starts_at = models.DateTimeField(blank=True, null=True)
class Child(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
parent = models.ForeignKey(Parent, on_delete=models.CASCADE, related_name='children')
starts_at = models.DateTimeField(null=True, blank=True)
And admin forms setup like:
class ChildInline(nested_admin.NestedTabularInline):
model = models.Child
extra = 0
@admin.register(models.Parent)
class ParentAdmin(nested_admin.NestedModelAdmin):
inlines = [ChildInline]
How would I validate the child based on the parent (or vice-versa)?
So far I've explored:
Form.clean()
- but this doesn't include the child/parent instances.Formset.clean()
- but despite making formsets it appears thatdjango-nested-admin
ignores them and their clean methods are never used.
Has anyone found a solution for this kind of issue?
It appears you can still use the
model.clean()
method for form validation:Which will make
ValidationError
appear on theChild
form. Using theclean
method on theParent
form is also possible, and would give the errors at that level.