Is there a way to determine which fields in the model are editable in the admin application?

Assume the following:

models.py

class Entry(models.Model):
    title = models.CharField(max_length=50)
    slug = models.CharField(max_length=50, unique=True)
    body = models.CharField(max_length=200)

admin.py

class EntryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug':('title',)}

I want the slug to be pre-populated with a header, but I don't want the user to be able to edit it with the administrator. I assumed that adding the = [] fields to the admin object and not including slug would work, but it is not. I also tried setting editable = False in the model, but that also did not work (infact, stops the page from rendering).

Thoughts?

+3
source share
5 answers

, slugify ( , django) slug. , - , .

:

def save(self):
    from django.template.defaultfilters import slugify

    if not self.slug:
        self.slug = slugify(self.title)

    super(Your_Model_Name,self).save()
+4

, . , , , , , , , ...

0

Django Snippet , , " ". , , .

0

This snippet gives you an AutoSlugField with the same behavior you are looking for, and adding it to your model is single-line.

0
source

In addition to the redefinition saveof the value of the generated, you can also use the option to exclude in ModelAdminorder to prevent the display of the field in the admin:

class EntryAdmin(admin.ModelAdmin):
    exclude = ('slug',)
0
source

Source: https://habr.com/ru/post/1697672/


All Articles