Ich hatte vorher Modelle wie folgt aus:Django inlinemodeladmin Validierung - aber mit einer allgemeinen Beziehung
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.')
Der Effekt war, ein neues AssemblyAnnotation (über eine Inline-Anlage) konnte nur eine Teilmenge von Werten für seine type-Attribut , abhängig von der übergeordneten Assembly.
Das hat super funktioniert.
Nun hat es kommen Zeit, diese Anmerkungen auf andere Objekte anzuwenden, die etwas anders sind:
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.')
Wie Sie sehen können, möchte ich die gleichen Regeln angewendet. Es gibt jedoch ein Problem. Die GenericInline, die ich jetzt verwende, legt nicht den self.content_type fest, bevor meine clean() -Methode ausgeführt wird.
Gibt es einen Weg um dies zu umgehen? Mache ich das falsch?
Danke für die Hilfe.
Nein, get_for_model [gibt ein einzelnes ContentType-Objekt zurück] (https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#django.contrib.contenttypes.models.ContentTypeManager.get_for_model). Wie auch immer, das ist nicht das Problem. Das Problem ist, dass GenericInline den Inhaltstyp vor dem Aufruf von clean() nicht festlegt. –