Changes between Version 8 and Version 9 of CookBookCategoryDataModel


Ignore:
Timestamp:
Aug 21, 2005, 7:28:48 AM (19 years ago)
Author:
aCC
Comment:

fixed code so that moving categories works correctly. had to change search_fields et. al. Hope that doesn't lose functionality.

Legend:

Unmodified
Added
Removed
Modified
  • CookBookCategoryDataModel

    v8 v9  
    1313    fields = (
    1414        meta.CharField('category_name', maxlength=50),
    15         meta.CharField('category_parents', maxlength=250, editable=False),
    16         meta.ForeignKey('self', blank=True, null=True,
     15        meta.ForeignKey('self', blank=True, null=True,
    1716            rel_name='parent', related_name='child'),
     17        meta.ForeignKey(User)
    1818    )
    19     unique_together = (("category_name", "category_parents"),)
    2019
    2120    def _recurse_for_parents(self, cat_obj):
     
    2726            p_list.extend(more)
    2827        if cat_obj == self and p_list:
    29 
    3028            p_list.reverse()
    3129        return p_list
     
    3735        p_list = self._recurse_for_parents(self)
    3836        return self.get_separator().join(p_list)
     37    _parents_repr.short_description = "Category parents"
    3938
    4039    def __repr__(self):
    41         if self.category_parents:
    42             l = [self.category_parents, self.category_name]
    43             return self.get_separator().join(l)
    44         return self.category_name
    45 
    46     def _pre_save(self):
    47         self.category_parents = self._parents_repr()
     40        p_list = self._recurse_for_parents(self)
     41        p_list.append(self.category_name)
     42        return self.get_separator().join(p_list)
    4843
    4944    admin = meta.Admin(
    50         list_display = ('category_name', 'category_parents'),
    51         search_fields = ['category_name', 'category_parents'],
     45        list_display = ('category_name', '_parents_repr'),
     46        search_fields = ['category_name'],
    5247    )
    53     ordering = ('category_parents', 'category_name')
    5448}}}
    5549
     
    119113http://www.djangoproject.com/documentation/models/m2o_recursive/
    120114
    121 == Comments ==
    122 
    123 This example has a bug: the field "category_parents" gets not updated, if a parent category was moved.
    124 
    125 E.g. you create the following categories:
    126 {{{
    127 firstcat
    128 secondcat
    129 secondcat :: thirdcat
    130 }}}
    131 
    132 
    133 Now you move "secondcat" under "firstcat". The correct display would be:
    134 {{{
    135 firstcat
    136 firstcat :: secondcat
    137 firstcat :: secondcat :: thirdcat
    138 }}}
    139 
    140 
    141 But because "category_parents" of thirdcat was not updated, you see:
    142 {{{
    143 firstcat
    144 firstcat :: secondcat
    145 secondcat :: thirdcat
    146 }}}
    147 
    148 
    149 In the database it is correct, just the display is incorrect.
    150 
    151 Can anybody fix this?
Back to Top