Is it possible to instantiate a model (create an object) and flag the instance as unsaveable?

21 views
Skip to first unread message

Bernd Wechner

unread,
Jun 2, 2018, 9:14:06 PM6/2/18
to Django users
I'd like to instantiate a model, and play around with it, but ensure it's never saved. At bare minimum instantiate it, play around with it then delete it, but ideally disable saving somehow. I imagine I can override save() on the instance, but am wondering if anyone has experience doing this or if there's a canonical method for doing it. I'll experiment but in the mean time fishing as I said for experience and any standard methods out there.

Regards,

Bernd.

James Bradshaw

unread,
Jun 2, 2018, 10:04:34 PM6/2/18
to django...@googlegroups.com
Hey Bernd,

I'm curious about your use case. It's common to create a read-only database user to make sure no data is modified, but I've never come across anything at the instance/row level.

-Jimmy

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c14ca134-cc54-498a-b5fa-78e80b768102%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Jun 3, 2018, 1:06:41 AM6/3/18
to django...@googlegroups.com


On Sat, Jun 2, 2018, 6:14 PM Bernd Wechner <bernd....@gmail.com> wrote:
I'd like to instantiate a model, and play around with it, but ensure it's never saved. At bare minimum instantiate it, play around with it then delete it, but ideally disable saving somehow. I imagine I can override save() on the instance, but am wondering if anyone has experience doing this or if there's a canonical method for doing it. I'll experiment but in the mean time fishing as I said for experience and any standard methods out there.

One way to make an obvious indication that the model instance is read-only is to use a proxy model of the original, with all the saving bits turned off. Keeps a nice separation of code while utilizing inheritance, and allows usage of the original/standard behavior.

IMO, overriding the save() method in the proxy model is probably the cleanest way to prevent an accidental save by a signal, etc.


-James

Bernd Wechner

unread,
Jun 4, 2018, 1:47:59 AM6/4/18
to Django users
Thanks James, I like that idea. I'll experiment with it. Have used Proxies before just not with this in mind.
Reply all
Reply to author
Forward
0 new messages