On Jan 12, 2008 5:28 AM, David Grant <[EMAIL PROTECTED]> wrote: > I have a simple category model. They will eventually be chapters in a book > and I only want entries to go into child categories, not the roots. Here's > the models: > > class Category(models.Model): > name = models.CharField (max_length=120, blank=True) > parent = models.ForeignKey('self', null=True, blank=True, > related_name='child_set') > > class Content(models.Model): > category = models.ForeignKey(Category) > > So say the categories were like this: > > Languages > -python > -ruby > -C++ > Frameworks > -django > -rails > -j2ee > > I don't want any content to go into the languages or frameworks > categories, only the child ones. Where would the logic to restrict the > choice of category in Content within the admin interface? >
Have you looked at the limit_choices_to argument for a ForeignKey ( http://www.djangoproject.com/documentation/model-api/#many-to-one-relationships )? > My best idea so far is to create a sub-class of ForeignKey called > ContentCategoryField and override the validate method. In the validate > method I could look up the id that was entered and see that has a parent id > or not. > > I was also thinking of restricting my categories to be just 2 levels, like > above. I could create a a ParentForCategoryField that overrides ForeignKey > and it could check to make sure that any new category being created does not > have a parent that also has a parent. > > Not sure if I'm on the right track here for a DRY method that will work > with the admin interface and also with newforms (should I decide to make > some of my own admin-like views). Not sure if limit_choices_to will be DRY, since it's admin-specific, but it seems designed to do what you are looking for in the admin interface, at least. Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---