Add's updated and created fields to a model if mixed in.
Example that uses the name as the representation:
class Company(models.Model, UnicodeReprMixIn):
"""
A representation of a comic book company.
"""
name = models.CharField(max_length=255)
slug = models.SlugField()
logo = models.ImageField(upload_to=os.path.join('upload', 'company_logos'))
url = models.URLField(verify_exists=True)
_unicode = "name"
1 2 3 4 5 6 7 8 9 10 11 12 13 | from django.db import models
class UnicodeReprMixIn(object):
"""
Adds a unicode representation using self._unicode.
"""
def __unicode__(self):
"""
Unocode representation of this instance.
"""
return unicode(self.__getattribute__(self._unicode))
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
If you don't like the snippet please comment on what would make it better ;-).
#
Please login first before commenting.