django管理员的更多一个prepopulated_fields

时间:2018-09-09 17:52:45

标签: python django python-3.x django-2.1

我正在接受Django的培训,我希望将我的实际博客从Wordpress转换为Django。然后,我将尝试重新创建Wordpress结构的任何部分(或希望以任何方式实现此目的...)

我将其放入了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"

这在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)

如果我写一篇文章并添加 category keyconcept prepopulated_fields 仅针对 title 字段运行。我怎么了?

管理控制台运行良好,目前我还没有激活任何模板来显示我的文章。

1 个答案:

答案 0 :(得分:1)

来自Django Doc

  

prepopulated_fields 不接受DateTimeField ForeignKey OneToOneField ManyToManyField 字段。


此处 keyconcept category ManyToManyField ForeignKey

因此 这是不可能的。 (AFAIK)