下面是简化的设置:
- A model based on legacy data which can t be changed. Therefore I raise a ValidationError to make the user aware that there was no change made. The form fields are readonly and I could use a simple pass but I prefer to get the message that save() didn t do what it was intended to do instead of just do silently nothing.
- Now I m extending the legacy data with a 2nd model which should be editable. It is included it into the legacy model s ModelAdmin as inline. I could include the CommentModel itself as a ModelAdmin, but as the LegacyModel inherits lots of functionality from parent-classes this gets complicated and un-dry.
我只想在网上开展“勇敢”行动。 我认为,所有领域都只是读写的,因此,它们应当做罚款。 难道有人会给我以清洁的方式这样做吗?
class Legacy(models.Model):
legacyData = models.TextField()
def clean(self):
raise ValidationError("%s model is readonly." % self._meta.verbose_name.capitalize())
class Comment(models.Model):
legacy = models.OneToOneField(Legacy)
comment = models.TextField()
class LegacyAdmin(admin.ModelAdmin):
def __init__(self, *args, **kwargs):
self.readonly_fields = self.fields
super(LegacyAdmin, self).__init__(*args, **kwargs)
model = Legacy
inlines = (CommentInline, )
感谢你们的很多时间!