I previously had models like this:
class AssemblyAnnotation(models.Model):
assembly = models.ForeignKey(Assembly)
type = models.ForeignKey(AssemblyAnnotationType)
...
def clean(self):
from django.core.exceptions import ValidationError
if not self.type.can_annotate_aliases and self.assembly.alias_of_id is not None:
raise ValidationError('The selected annotation type cannot be applied to this assembly.')
The effect was, a new AssemblyAnnotation (attached via an inline) could only have a subset of values for it's type attribute, depending on the parent Assembly.
This worked great.
Now, it has come time to apply these annotations to other objects that are slightly different:
class ObjectAnnotation(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
type = models.ForeignKey(AssemblyAnnotationType)
...
def clean(self):
from django.core.exceptions import ValidationError
if self.content_type == ContentType.objects.get_for_model(Assembly):
if not self.type.can_annotate_aliases and self.content_object.alias_of_id is not None:
raise ValidationError('The selected annotation type cannot be applied to this assembly.')
As you can see, I want the same rules applied. However, there is a problem. The GenericInline that I am now using does not set self.content_type before my clean() method is run.
Is there any way around this? Am I doing this wrong?
Thanks for the help.