I'll second Christian Joergensen by saying that foreign key fields are
the way to go here, rather than choices hacks.  Put those types in
another table.

gsf

On Nov 20, 12:16 pm, David Marko <[EMAIL PROTECTED]> wrote:
> Using this I could create a static list only. But what I mean is
> dynamic list based on database query.
> I have the following model for keeping the list of different choices .
>
> class Choice(models.Model):
>     TYPES = (
>         ('os_type','OS Types'),
>         ('os_type','OS Types'),
>     )
>
>     type = models.CharField(verbose_name='Type', maxlength=30,
> choices=TYPES, null=False, blank=False)
>     value = models.CharField(verbose_name='Key', maxlength=30,
> null=False, blank=False)
>     label = models.CharField(verbose_name='Value', maxlength=30,
> null=False, blank=False)
>
>     @classmethod
>     def get_choices_for(self, type):
>         l=[(k.value,k.label) for k in self.objects.filter(type=type)]
>         l.extend([('','- select -'),])
>         l.sort()
>         return (l)
>
> .... then on the another model I would like to make specific choices
> available:
> type = models.CharField(verbose_name='OS', maxlength=30, choices =
> Choice.get_choices_for('os_type'), null=False, blank=False)
>
> ... but using this technique doesnt work as Django runs
> Choice.get_choices_for('os_type')  only one time when application
> starts up ... so it doesnt reflect changes made to Choices table.
>
> David
>
> On Nov 20, 4:57 pm, Michael <[EMAIL PROTECTED]> wrote:
>
> > The following worked for me:
>
> > model field:
> > type = models.CharField(max_length=10, radio_admin=True,
> > choices=CHOICE_LIST)
>
> > where CHOICE_LIST is a tuple of tuples, defined inside the model if
> > local, outside if you need it in multiple models,  like:
> > CHOICE_LIST = (('dbasename1', 'displayname1'), ('dbasename2',
> > 'displayname2'),)
>
> > radio_admin=True causes the list to be displayed as a radio select
> > list (instead of a select/drop list or something.)  The docs 
> > athttp://www.djangoproject.com/documentation/model-api/saythe
> > radio_admin is new in the developmental version.
>
> > Hope this helps,
> >  Michael
>
> > On Nov 20, 6:48 am, Christian Joergensen <[EMAIL PROTECTED]> wrote:
>
> > > David Marko wrote:
> > > > How to define choices for filed that works in admin interface? When I
> > > > define CharField as below, the list of choices is computed intially
> > > > and doesn't refresh when data changes in Choices table.
>
> > > > type = models.CharField(verbose_name='Type', maxlength=30, choices =
> > > > Choice.filter("xxxx"), null=False, blank=False)
>
> > > Normally, one would use a foreign key in this case:
>
> > > choice = models.ForeignKey(Choice)
>
> > > Regards,
>
> > > --
> > > Christian Joergensen | Linux, programming or web 
> > > consultancyhttp://www.razor.dk|    Visit us at:http://www.gmta.info
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to