On Tue, Dec 9, 2008 at 1:58 PM, lingrlongr <[EMAIL PROTECTED]> wrote:
> > I have a function called get_urls that returns a list of absolute_urls > for various models in the same project. This function is used for > another model in a way so that when a particular model appears on the > site, I can choose which part of the site this model links to. > > # utils.py > def get_urls(): > urls = [] > for flatpage in FlatPage.objects.order_by('title') > urls.append(flatpage.url, '%s: %s' % ('Flatpage', > flatpage.title),)) > return urls > > There are more objects, this is just a sample with the well-known > FlatPage model. > > The particular model I use this with looks like this: > > # models.py > from utils import get_urls > class BannerImage(models.Model): > image = models.ImageField(etc) > url = models.CharField(max_length=100, choices=get_urls()) > etc... > Here you have put your call to get_urls() in code that is executed only when the class is defined, not each time the class is instantiated. You probably want to read the last paragraph of the description of choices here: http://docs.djangoproject.com/en/dev/ref/models/fields/#choices > So it turned out that the admin seemed to be caching the choices. So, > if I added a new flatpage, it would not be listed there until I > restarted the webserver. So instead of referring to > Flatpage.objects.etc... directly in get_urls, I made a function that > returns the queryset instead: > Admin was not caching anything, it is Python that only executes the code once given where it had been placed. > # utils.py > def get_flatpages(): > return FlatPage.objects.order_by('title') > > Problem still seemed to exist. I tried making a custom ModelForm in > admin.py for this BannerImage model: > > # admin.py > from utils import get_urls > class BannerImageForm(forms.ModelForm): > url = forms.ChoiceField(choices=get_urls()) > class Meta: > model = BannerImage > Here you have still put the determination of choices into code that is only executed when the class (this time the form class) is defined. You could fix it for the form case by moving the setting of choices into an __init__ routine for the form. 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 -~----------~----~----~----~------~----~------~--~---