-1

trying to alter order of fields in admin ModelForm. Bellow is my attempt, however order is kept unchanged. Added fields oi_number and vat_number are rendered at the end besides they are not at the end in self.fields SortedDict dictionary.

class ContactAdminForm(forms.ModelForm):
    oi_number = fields_for_model(OrganizationExtra)['oi_number']
    vat_number = fields_for_model(OrganizationExtra)['vat_number']
    # fields = ('organization', 'oi_number', 'vat_number')
    # ^^^ this won't affect fields order either

    class Meta:
        model = Organization

    def __init__(self, *args, **kwargs):
        super(ContactAdminForm, self).__init__(*args, **kwargs)
        try:
            org_ix = self.fields.keyOrder.index('organization')
            self.fields.keyOrder.insert(org_ix+1, self.fields.keyOrder[-2])
            self.fields.keyOrder.insert(org_ix+2, self.fields.keyOrder[-1])
            del self.fields.keyOrder[-2:]
        except ValueError:
            pass

Does get the order of fields resolved before __init__ method is called ? How can I change their order ?

Update: The above ModelForm is used as a form in admin model which defines its own fields, so if I put all fields definition in above form, I'll get FieldError exception about unknown field name:

class ContactAdminForm(forms.ModelForm):
    ...
    class Meta:
        model = Organization
        fields = ('organization', 'oi_number', 'vat_number')

class ContactOptionsEx(ContactOptions):
    form = ContactAdminForm

admin.site.register(Contact, ContactOptionsEx)

# at attempt to render the form:
# FieldError at /admin/contact/contact/3/
# Unknown field(s) (organization) specified for Organization

However the field named organization does exist and is available in ContactAdminForm.__init__ method.

David Unric
  • 7,421
  • 1
  • 37
  • 65
  • Did you put the `fields = ...` under `Meta`? That should work... – sk1p Dec 15 '13 at 16:36
  • @sk1p if I do put fields in `Meta` class, then I'm getting `FieldError` exception. See the question update for details. – David Unric Dec 15 '13 at 17:41
  • @yuvi I believe the linked possible duplicate does not answer the question completely. – David Unric Dec 15 '13 at 17:51
  • Seeing your update, does this help you? http://stackoverflow.com/questions/350799/how-does-django-know-the-order-to-render-form-fields – yuvi Dec 15 '13 at 18:49
  • @yuvi Unfortunately no. Any changes in order, in form's `__init__` method have no effect. Only setting in `Meta` class. – David Unric Dec 16 '13 at 12:07

1 Answers1

2

The error

Unknown field(s) (organization) specified for Organization

does not refer to a field on your form, but to a field on the model (Organization).

I think the problem here is that you are trying to add fields from a different Model (OrganizationExtra) to the ModelForm for Organization. There is always a one-to-one relation between a ModelForm and a Model. If you want to edit a related instance in the admin, you can use inlines:

class OrganizationExtraInline(admin.StackedInline):
    model = OrganizationExtra

class ContactOptionsEx(ContactOptions):
    inlines = ContactOptions.inlines + [OrganizationExtraInline]
    # ...

If you want to limit the inline to one instance, use a OneToOneField or max_num = 1

sk1p
  • 6,645
  • 30
  • 35
  • You are right, I'm trying to add fields from a different model. I know I can use `StackedInline` or `TabularInline` classes to display fields of related models, however I'd like to insert those extra fields directly bellow `organization` field, ie. in-between default model's fields. – David Unric Dec 15 '13 at 18:35
  • I guess an option would be to use an inline on the python side but override the changeform template to reorder the fields… can't think of anything else right now. – sk1p Dec 15 '13 at 18:44
  • @DavidUnric so if it's so customized, you might want to drop django.forms and work it out yourself. I personally think django.forms is the weakest part of the django framework, and has many flaws. I found myself building my own (or using alternatives) more than once even when the customization seemed simple at a glance – yuvi Dec 16 '13 at 12:10