views:

56

answers:

3

How can I set a field in admin to be at the same time non-editable and prepopulated from other field ?

+1  A: 

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields

eruciform
readonly works only for 1.2 on on my server I have 1.1 . I've tried 'editable=False' in my model field declaration, but together with prepopulate it raises an error.
markus glock
A: 

django-autoslug might be helpful.

ex:

from autoslug.fields import AutoSlugField

class FooModel(models.Model):
    title = models.CharField(max_length=200)
    pub_date = models.DateField(auto_now_add=True)
    slug = AutoSlugField(populate_from='title', unique_with='pub_date__month')
Ashok
+1  A: 

From what I understand, prepopulate is there to prepopulate an editable field in the admin form. If you want to have one field's content automatically generated based on another's, then prepopulate is not what you want.

The way I usually do this is by setting the field to be non-editable, not prepopulating it, and overriding the model's save() method to get the value from the other field as necessary.

For automatic unique slug generation, Ashok's suggestion is the way to go.

Lexo
I've found a better solution for generating slugs - http://trac.django-fr.org/browser/site/trunk/project/links/slughifi.py?rev=47
markus glock
@markus That's a really cool extension to slugify. Thanks for letting me know about it!
Lexo