On 8 Mar 2010, at 09:01 , JF Simon wrote: > > Hi dudes, > > My object seems mysterious, as is my problem ... I'd like to > understand something (it's much more related to Python than Django). > > Take an example : > > class Image(models.Model): > email = models.ForeignKey('Email', verbose_name=_(u'Email')) > name = models.CharField(_(u'Name'), max_lenth=50, > help_text=_(u'Must be unique for a given email')) > content_id = models.CharField(_(u'Content ID'), max_length=25, > db_index=True, default=self.slugify_name()) > [...] > > "self" is not defined in the "default" kwarg for "content_id" field, > it's because I'm not in an instance method (with self as first > parameter). So what are those members, are they static for the class ? > What is the difference between them and a member instantiated in the > __init__ method whith self.field = [...] ? > > If I replace the last field definition with : > > from django.template.defaultfilters import slugify > [...] > content_id = models.CharField(_(u'Content ID'), max_length=25, > db_index=True, default=slugify(name)) > > Eclipse don't show me an error ... will it work ? Why (not) ? I'm not > sure to understand what I'm doing and it's weird. It will 'work', but I doubt it's going to do what you want.
Here are how python class definitions work: 1. The body of the class (anything after class YourName:) is parsed and executed sequentially, as if it were module-level Python code (which is why you can have a class inside a class, for instance) 2. *After* the body of the class has executed, all of its content (the variables local to that body, which you can see by writing `print locals()` at the end of the class body) is collected and set as *class attributes*, with functions turned into methods. Because a class body is "just code", you can: 1. Use things defined outside the class body (models or slugify, in your second snippet) 2. *Reference things previously defined in the body* (such as `name`). Finally, you might very well be doing unnecessary work: Django already provides a SlugField (http://docs.djangoproject.com/en/dev/ref/models/fields/#slugfield) which — in the admin — can be prepopulated from your name using prepopulate_field (http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields). If you want to set it in any case, you'll have to use __init__ I believe, but I'm sure others will be able to infirm that and show a better way. But in this case, `slugify` is going to get passed a CharField, which probably isn't what you want. Finally, > So what are those members, are they static for the class ? Well they are class attributes, but they're also instance attributes. They're one of those very-useful-but-not-necessaril-super-clear features: descriptors. See http://users.rcn.com/python/download/Descriptor.htm for a very good howto by Raymond Hettinger. Note that you could just as well go with "magic, gotcha" for now on that subject. They're basically class attributes which do special stuff when you invoke them from an instance. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.