Login

UnicodeReprMixIn

Author:
ashcrow
Posted:
January 4, 2009
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 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.