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
-~----------~----~----~----~------~----~------~--~---

Reply via email to