background preloader

Django

Facebook Twitter

USER 01. Welcome to Python.com. Model instance reference. A few object methods have special purposes. __unicode__ Model. __unicode__() The __unicode__() method is called whenever you call unicode() on an object. Django uses unicode(obj) (or the related function, str(obj)) in a number of places. For example: class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) def __unicode__(self): return u'%s %s' % (self.first_name, self.last_name) If you define a __unicode__() method on your model and not a __str__() method, Django will automatically provide you with a __str__() that calls __unicode__() and then converts the result correctly to a UTF-8 encoded string object. Get_absolute_url Model.get_absolute_url() Define a get_absolute_url() method to tell Django how to calculate the canonical URL for an object.

Def get_absolute_url(self): return "/people/%i/" % self.id (Whilst this code is correct and simple, it may not be the most portable way to write this kind of method. <! Note. Django signals | Mercurytide. Introduction If you are one of the lucky ones, you're using the Django framework. Sometimes in life it's nice to sit back, relax, and let the world come to you. Why rush around to find out what's happening? Wouldn't it be nice to be notified when the things you're interested in are happening? When programming, similar thoughts periodically rise up in a programmer's mind. "Hey! If you are one of the lucky ones, you're programming web sites in Python using the Django framework. In Django parlance, this is known as signals, and it's just one of the many reasons why Django makes web development so much more, well, fun.

The aim of this article is to introduce Django signals and cover their uses in detail. Introduce the concept of signalling;state what Django signals are and why we need them;provide examples of listening for Django's built-in signals; andshow you how to send your own signals. This article assumes you are comfortable with Python programming and the Django web framework. Summary.

Apps

Writing custom model fields. When planning your Field subclass, first give some thought to which existing Field class your new field is most similar to. Can you subclass an existing Django field and save yourself some work? If not, you should subclass the Field class, from which everything is descended. Initializing your new field is a matter of separating out any arguments that are specific to your case from the common arguments and passing the latter to the __init__() method of Field (or your parent class). In our example, we’ll call our field HandField. Our HandField accepts most of the standard field options (see the list below), but we ensure it has a fixed length, since it only needs to hold 52 card values plus their suits; 104 characters in total.

All of the options without an explanation in the above list have the same meaning they do for normal Django fields. Useful methods Custom database types Field.db_type(self, connection) New in Django 1.2: The connection argument was added to support multiple databases. Django.db.models.fields.files.FieldFile. Inherited from core.files.base.File: __iter__, __len__, __nonzero__, __repr__, __str__, __unicode__, chunks, multiple_chunks Inherited from object: __delattr__, __format__, __getattribute__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Index — Django Design Patterns.