如何使用选项中的查询在django admin中设置子页面

时间:2021-09-17 18:48:25

I'm trying to make a selector to set a page as subpage. The code I have now doesn't work. I want to store the id of the parent if the page is set as subpage. I think my way doesn't make sense but i don't know another solution. The error is "No module named pages". Thanks for helping :D

我正在尝试创建一个选择器来将页面设置为子页面。我现在的代码不起作用。如果页面设置为子页面,我想存储父ID的ID。我认为我的方式没有意义,但我不知道另一种解决方案。错误是“没有模块命名的页面”。谢谢你的帮助:D

import datetime
from django.db import models
from django.utils import timezone
from ckeditor.fields import RichTextField
from django.template.defaultfilters import slugify
from autoslug import AutoSlugField
from models.pages import Page

class Page(models.Model):
    all_objects = Page.objects.all()
    parent_page = [(0, 'Main page')]
    parent_page = parent_page.extend([(x.id, x.page_title) for x in all_objects])

    page_title = models.CharField(max_length=200)
    admin_title = models.CharField(max_length=200, blank = True)
    sub_page = models.IntegerField(default = 0, choises = parent_page)
    content = RichTextField(blank = True)
    slug = AutoSlugField(blank = True, populate_from='page_title', unique=True, editable=True)
    pub_date = models.DateTimeField('date published')
    view_status = models.IntegerField(default=1, choices=[(1, 'Active'), (2, 'Active, not displayed in menu'), (3, 'Inactive')])

    def __str__(self):
        return self.page_title

1 个解决方案

#1


0  

I'd implement what you want this way:

我会用这种方式实现你想要的东西:

import datetime

from django.db import models
from django.template.defaultfilters import slugify
from django.utils import timezone

from autoslug import AutoSlugField
from ckeditor.fields import RichTextField

VIEW_STATUS_CHOICES = [
    (1, 'Active'), 
    (2, 'Active, not displayed in menu'), 
    (3, 'Inactive')
]

class Page(models.Model):
    parent_page =  models.ForeignKey('self')
    page_title = models.CharField(max_length=200)
    admin_title = models.CharField(max_length=200, blank = True)
    sub_page = models.IntegerField(default = 0, choices = parent_page)
    content = RichTextField(blank = True)
    slug = AutoSlugField(blank = True, populate_from='page_title', 
                         unique=True, editable=True)
    pub_date = models.DateTimeField('date published')
    view_status = models.IntegerField(default=1, choices=VIEW_STATUS_CHOICES)

    #Some helper methods
    def get_children(self):
        return Page.objects.filter(parent_page=self)

    def has_children(self):
        return get_children.count() > 0

    def __str__(self):
        return self.page_title

#1


0  

I'd implement what you want this way:

我会用这种方式实现你想要的东西:

import datetime

from django.db import models
from django.template.defaultfilters import slugify
from django.utils import timezone

from autoslug import AutoSlugField
from ckeditor.fields import RichTextField

VIEW_STATUS_CHOICES = [
    (1, 'Active'), 
    (2, 'Active, not displayed in menu'), 
    (3, 'Inactive')
]

class Page(models.Model):
    parent_page =  models.ForeignKey('self')
    page_title = models.CharField(max_length=200)
    admin_title = models.CharField(max_length=200, blank = True)
    sub_page = models.IntegerField(default = 0, choices = parent_page)
    content = RichTextField(blank = True)
    slug = AutoSlugField(blank = True, populate_from='page_title', 
                         unique=True, editable=True)
    pub_date = models.DateTimeField('date published')
    view_status = models.IntegerField(default=1, choices=VIEW_STATUS_CHOICES)

    #Some helper methods
    def get_children(self):
        return Page.objects.filter(parent_page=self)

    def has_children(self):
        return get_children.count() > 0

    def __str__(self):
        return self.page_title