MaxDragonheart
MaxDragonheart

Reputation: 1300

More of one prepopulated_fields for django admin

I'm in training with Django and I would to convert my actual blog from Wordpress to Django. Then I will try to recreate any part of Wordpress structure(or at any rate I hope to do this...)

I have this into models.py:

class KeyConcept(models.Model):
    text = models.CharField(max_length=50, verbose_name="Concetti chiave", help_text="Every key concept must be not longer then 50 characters")
    slug = models.SlugField(verbose_name="Slug", unique="True", help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents")

    def __str__(self):
        return self.text

    def get_absolute_url(self):
        return reverse("keyconceptView", kwargs={"slug": self.slug})

    class Meta:
        verbose_name = "Concetto chiave"
        verbose_name_plural = "Concetti chiave"



class Argument(models.Model):
    type = models.CharField(max_length=20, verbose_name="Categoria", help_text="Every argument must be not longer then 20 characters")
    slug = models.SlugField(verbose_name="Slug", unique="True", help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents")

    def __str__(self):
        return self.type

    def get_absolute_url(self):
        return reverse("argumentView", kwargs={"slug": self.slug})

    class Meta:
        verbose_name = "Categoria"
        verbose_name_plural = "Categorie"



class Post(models.Model):
    title = models.CharField(max_length=70, help_text="Write post title here. The title must be have max 70 characters", verbose_name="Titolo")
    short_description = models.TextField(max_length=200, help_text="Write a post short description here. The description must be have max 200 characters", verbose_name="Breve descrizione dell'articolo")
    contents = models.TextField(help_text="Write your post here", verbose_name="Contenuti")
    publishing_date = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="Data di pubblicazione")
    updating_date = models.DateTimeField(auto_now=True, verbose_name="Data di aggiornamento")
    category = models.ForeignKey(Argument, on_delete=models.CASCADE, related_name="connected_argument", verbose_name="Categoria")
    keyconcept = models.ManyToManyField(KeyConcept, related_name="connected_keyconcept", verbose_name="Concetti chiave")
    slug = models.SlugField(verbose_name="Slug", unique="True", help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents")
    highlighted = models.BooleanField(default=False, help_text="If you want that the post went be highlighted, click on this area", verbose_name="Articolo in evidenza")

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("singlearticleView", kwargs={"slug": self.slug})

    class Meta:
        ordering = ['-publishing_date']
        verbose_name = "Articolo"
        verbose_name_plural = "Articoli"



class Post(models.Model):
    title = models.CharField(max_length=70, help_text="Write post title here. The title must be have max 70 characters", verbose_name="Titolo")
    short_description = models.TextField(max_length=200, help_text="Write a post short description here. The description must be have max 200 characters", verbose_name="Breve descrizione dell'articolo")
    contents = models.TextField(help_text="Write your post here", verbose_name="Contenuti")
    publishing_date = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="Data di pubblicazione")
    keyconcept = models.ManyToManyField(KeyConcept, related_name="connected_keyconcept", verbose_name="Concetti chiave")
    slug = models.SlugField(verbose_name="Slug", unique="True", help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents")
    highlighted = models.BooleanField(default=False, help_text="If you want that the post went be highlighted, click on this area", verbose_name="Articolo in evidenza")

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("singlepostManuscriptusView", kwargs={"slug": self.slug})

    class Meta:
        ordering = ['-publishing_date']
        verbose_name = "Articolo"
        verbose_name_plural = "Articoli"

And this in admin.py:

class PostAdmin(admin.ModelAdmin):
    list_display = ["id", "__str__", "publishing_date", "updating_date", "category", "highlighted"]
    list_filter = ["publishing_date"]
    search_fields = ["title", "short_description", "contents", "keyconcept", "category"]
    prepopulated_fields = {"slug": ("title", "keyconcept", "category",)}

    class Meta:
        model = Post

admin.site.register(Post, PostAdmin)
admin.site.register(KeyConcept)
admin.site.register(Argument)

If I write an article and add category and keyconcept prepopulated_fields runs only for title field. What I've wrong?

Admin panel running well and for now I've not activate any templates for show my articles.

Upvotes: 0

Views: 5946

Answers (1)

JPG
JPG

Reputation: 88649

From the Django Doc

prepopulated_fields doesn’t accept DateTimeField, ForeignKey, OneToOneField, and ManyToManyField fields.


Here keyconcept and category are ManyToManyField and ForeignKey respectively.

Hence it's not possible. (AFAIK)

Upvotes: 4

Related Questions