User64522
User64522

Reputation: 3

Django Admin: How to only save inline model not parent

I have following simplified setup:

What I want is to perform the "save" operation only on the inline-model. I thought as all fields are readonly it should work fine. Can someone give me a hint to do this in clean way?

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, )

Thanks a lot for your time! :)

Upvotes: 0

Views: 1026

Answers (1)

Danny W. Adair
Danny W. Adair

Reputation: 12968

Rather than raising an exception in clean(), you could override the legacy's save() and use http://docs.djangoproject.com/en/dev/ref/contrib/messages/ to tell your user what didn't happen.

Upvotes: 1

Related Questions