I use the same as you, and it works as expected. Here is my code:
In models:
class GroupFlagIntermediate(models.Model): group_flag = models.ForeignKey(GroupFlag, related_name='flag_set') content_type = models.ForeignKey(ContentType, verbose_name='Flag Type') flag_pk = models.CharField('Flag PK', max_length=100, blank=True, default='') flag = generic.GenericForeignKey('content_type', 'flag_pk') def clean(self): from django.core.exceptions import ValidationError if not self.is_valid_flag(self.content_type.model_class()): raise ValidationError('The selected flag is not a real Flag.')
And in admin:
class GroupFlagIntermediateInline(admin.TabularInline): model = GroupFlagIntermediate class GroupFlagAdmin(admin.ModelAdmin): list_display = ('name', ...) inlines = [GroupFlagIntermediateInline] admin.site.register(GroupFlag, GroupFlagAdmin)
After some tests, I found that the fields content_type and object_id ( flag_pk in my case) are set before calling clean (), but GenericForeignKey ( flag in my case) doesnβt.
Nicolas malbran
source share