开发者

(Django) prepopulate field, and forbid editing

How can I set a field in admin to be at the same time non-editable and prepopulated fro开发者_开发百科m other field ?


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')


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜