Hi there, I think what you've figured out is something along the same lines that I'd like to do - but I'm very new to both Python and Django, so please pardon my ignorance in that I really didn't understand the solution you two spoke about.
Here's what I'd like to do... I've two models, one foriegnKey'ed to the other. in models.py: class Market(models.model): name = models.CharField(max_length=55) class Alias(models.model): market_id = models.ForeignKey(Market) alias = models.CharField(max_length=255) uripatern = models.CharField(max_length=255) in admin.py: class AliasAdmin(admin.ModelAdmin): list_display = ('market_name', 'alias', 'uripatern') list_filter = ['market_id'] search_fields = ['alias', 'uripatern', 'market_id__name'] So as you can see, I'd like to include the Market.name field rather than the market_id, but I get "None" listed in the Admin display for the Alias. I've tried to add a function in the class Alias: def market_name(self): return self.market_id__name But this just shows "None" in the admin screen for Aliases. Thank you for your time and any help. On Mar 11, 1:23 pm, Alex Jillard <mez...@gmail.com> wrote: > Ah, that makes sense, thanks. With that change, I can use _meta.fields from > my other model and everything works great. > > Thanks for the help > > On Wed, Mar 11, 2009 at 12:53 PM, Alex Gaynor <alex.gay...@gmail.com> wrote: > > > On Wed, Mar 11, 2009 at 11:50 AM, Alex Jillard <mez...@gmail.com> wrote: > > >> If I override get_form in ShelfAdmin, I still get the same errors. > >> self.fields is still None...any idea how to get access to the fields? > > >> On Wed, Mar 11, 2009 at 12:26 PM, Alex Gaynor <alex.gay...@gmail.com>wrote: > > >>> On Wed, Mar 11, 2009 at 11:18 AM, Alex Jillard <mez...@gmail.com> wrote: > > >>>> Sorry Alex, I should have been more clear, I need to access the fields > >>>> of another model. I went ahead and did what you said to try and get it > >>>> working, and I was able to get the fields from the form's model, but I > >>>> get > >>>> an error when trying to assign them to the choices property of my field. > > >>>> The following code gives the following error: > > >>>> class ShelfAdmin(admin.ModelAdmin): > >>>> sort_by = forms.ChoiceField(choices = ()) > > >>>> def __init__(self, *args, **kwargs): > >>>> super(ShelfAdmin, self).__init__(*args, **kwargs) > > >>>> sort_by_choices = [('', '-------')] > > >>>> for field in self.model._meta.fields: > >>>> sort_by_choices.append((field.name, > >>>> field.verbose_name.title())) > > >>>> self.fields['sort_by'].choices = sort_by_choices > > >>>> admin.site.register(Shelf, ShelfAdmin) > >>>> TypeError at /admin/shelf/shelf/add/ > > >>>> 'NoneType' object is unsubscriptable > > >>>> Which I guess means that self.fields is None. Any idea why this might > >>>> be happening? > > >>>> On Wed, Mar 11, 2009 at 11:35 AM, Alex Gaynor > >>>> <alex.gay...@gmail.com>wrote: > > >>>>> On Wed, Mar 11, 2009 at 10:33 AM, Alex Jillard <mez...@gmail.com>wrote: > > >>>>>> I'm trying to populate an admin form with the field names of of a > >>>>>> model class, but I don't want to have to instantiate that model just > >>>>>> to read > >>>>>> it's fields from _meta. > > >>>>>> I basically want to do something like this: > > >>>>>> self.fields['sort_by'].choices = [(field.verbose_name, field.name) > >>>>>> for field in field_list] > > >>>>>> field_list would be the equivalent to model_instance._meta.fields, but > >>>>>> without requiring the model instance. > > >>>>>> I know I could just grab an instance of the model from the database, > >>>>>> but I'd like this to work even if there are no instances currently > >>>>>> saved. > > >>>>> _meta exists on the class as well, so on a ModelAdmin you can do > >>>>> self.model._meta.fields > > >>>>> Alex > > >>>>> -- > >>>>> "I disapprove of what you say, but I will defend to the death your > >>>>> right to say it." --Voltaire > >>>>> "The people's good is the highest law."--Cicero > > >>> This occurs because self.fields isn't a dictionary of the field objects > >>> like it is on a ModelForm, you probably want to do all of this in the > >>> get_form() method. > > >>> Alex > > >>> -- > >>> "I disapprove of what you say, but I will defend to the death your right > >>> to say it." --Voltaire > >>> "The people's good is the highest law."--Cicero > > > It's not self.fields you want to assign to, it's the Form class the you get > > from calling the super(and it will actually be base_fields here since you're > > dealing with a Form class instead of an instance). > > > Alex > > > -- > > "I disapprove of what you say, but I will defend to the death your right to > > say it." --Voltaire > > "The people's good is the highest law."--Cicero --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---