Login

UnicodeReprMixIn

Author:
ashcrow
Posted:
January 4, 2009
Language:
Python
Version:
1.0
Tags:
model unicode orm mixin __unicode__
Score:
-2 (after 6 ratings)

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

  1. RelatedMixin for Details and Updates with Related Object Lists by christhekeele 2 years, 12 months ago
  2. Display arbitrary models by bjornkri 6 years, 10 months ago
  3. model queries to Google Visualization DataTables by banderkat 1 year, 10 months ago
  4. Inspect object debugging tag by dballanc 7 years, 10 months ago
  5. Model inheritance with content type by crucialfelix 6 years, 8 months ago

Comments

ashcrow (on January 18, 2009):

If you don't like the snippet please comment on what would make it better ;-).

#

Please login first before commenting.