嵌套类别/ InlinePanel(s)witctail

时间:2015-11-11 13:37:09

标签: wagtail

我很难实现像#34;嵌套类别":

这样的东西
PageA:
  - Cat1
    - SubCat1
    - SubCat2
    - ...
  - Cat2
    - SubCat1
  - ...

所有类别和子类别都应该由编辑器订购和编辑。

我的猜测是这样的:

class CategoryTestPage(Page):
    content_panels = Page.content_panels + [
        InlinePanel('categories')
    ]


class Category(Orderable,ClusterableModel,models.Model):
    page = ParentalKey(CategoryTestPage, related_name='category')
    category = models.CharField(max_length=250)

    def __str__(self):
        return "%d %s" % (self.id, self.category)

    panels = [
            FieldPanel('category'),
            InlinePanel('subcategory')
    ]

class SubCategory(Orderable,models.Model):
    category = ParentalKey(ProjektOrdnung, related_name='subcategory')
    subcategory = models.CharField(max_length=250)

    def __str__(self):
        return "%d %s" % (self.id, self.subcategory)

    panels = [
            FieldPanel('subcategory')
    ]

但这导致'CategoryForm' object has no attribute 'formsets'。似乎嵌套InlinePanel是问题吗?

此外,我需要这个"等级分类法"用于将其中一些类别/子类别分配给其他页面:

PageB:
    - has Cat1
      - has SubCa2
    - ...

......看起来很像分层标签...

任何想法如何实现这个或我的实施有什么问题?

亲切的问候, tombreit

PS:我在wagtail 1.2rc1

1 个答案:

答案 0 :(得分:3)

这是一种方法,有很大的界面改进空间;)为了在页面级别对类别进行排序,我建议使用django-sortedm2m

public static void Main()
{
    var deck = new BlackJackDeck();

    deck.Shuffle();

    foreach (var card in deck.Cards)
    {
        Console.WriteLine(card.ToString());
    }
}
public class BlackJackDeck
{
    private const int DECKSIZE = 52;
    private Random random;

    //Card is your key, and int is the value of the card
    public IList<BlackJackCard> Cards { get; private set; }


    public BlackJackDeck()
    {
        random = new Random();
        NewDeck();
    }

    public void Shuffle()
    {
        //Documentation: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
        for (var i = 0; i < Cards.Count; i++)
        {
            var position = random.Next(i);
            var temp = Cards[i];
            Cards[i] = Cards[position];
            Cards[position] = temp;
        }
    }

    #region Helper Methods

    private void NewDeck()
    {
        Cards = new List<BlackJackCard>(DECKSIZE);

        for (var i = 0; i < 4; i++)
        {
            for (var j = 0; j < 13; j++)
            {
                switch (j)
                {
                    case 8:
                    case 9:
                    case 10:
                    case 11:
                        Cards.Add(new BlackJackCard { Suit = (Suit)i, Face = (CardFace)j, Value = 10});
                        break;
                    case 12:
                        Cards.Add(new BlackJackCard { Suit = (Suit)i, Face = (CardFace)j, Value = 11});
                        break;
                    default:
                        Cards.Add(new BlackJackCard { Suit = (Suit)i, Face = (CardFace)j, Value = (j + 2)});
                        break;
                }
            }
        }
    }

    #endregion Helper Methods
}

public enum Suit
{
    Spades,
    Hearts,
    Clubs,
    Diamonds
}

public enum CardFace
{
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King,
    Ace
}

public class BlackJackCard : Card
{
    public int Value { get; set; }

    public override string ToString()
    {
        //MSDN Documentation: https://msdn.microsoft.com/en-us/library/dn961160.aspx
        //base.ToString() is the Card classes ToString()
        return $"{base.ToString()} with value: {Value}";
    }
}

//this is for making a basic card and deck of cards
//Inherit from this to create cards specific to different game rules, i.e BlackJack, Go Fish, etc
//Documentation: https://msdn.microsoft.com/en-us/library/k535acbf(v=vs.71).aspx
public abstract class Card
{
    public Suit Suit { get; set; }
    public CardFace Face { get; set; }

    public override string ToString()
    {
        return $"{Face} of {Suit}";
    }
}