I'm trying to send an email to a user when a new model instance is saved and I want the email to include a link to the admin page for that model instance. Is there a way to get the correct URL? I figure Django must have that information stored somewhere.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
This gives the same result as Josvic Zammit's snippet, but does not hit the database:
Just use this one liner that is also python 3 ready:
More on this in the django admin site doc, reversing admin urls.
This Django snippet should do:
The
self
refers to the parent model class, i.e.self.id
refers to the object's instanceid
. You can also set it as aproperty
on the model by sticking the@property
decorator on top of the method signature.Not trying to rip off @JosvicZammit, but using
ContentType
is the wrong approach here. It's just a wasted DB query. You can get the require info from the_meta
attribute:So, combining the answers by Chris, Josvic and Josh, here's a copy-paste method you can add into your model (tested on Django 1.8.3).