Repository: django-import-export/django-import-export

Issue:
ModelResource field order in admin import page 
**Describe the bug**
Hello, I'm in the process of upgrading from 3.3.9 to 4.0.3 and noticed that the order of fields displayed in the admin import view differs from that of the `fields` definition in the resource's `Meta` class. Specifically, fields defined using an `attr=<foreign_key>` get placed at the end of the fields list, regardless of whether you define `import_order` or not.

For example, given this resource:

```python
class PlanElevationResource(ModelResource):
    job = fields.Field(column_name="job", attribute="job")  # ForeignKey
    plan = fields.Field(column_name="plan", attribute="plan") # CharField
    elevation = fields.Field(column_name="elevation", attribute="elevation") # CharField
    notes = fields.Field(column_name="notes", attribute="notes") # TextField

    class Meta:
        model = PlanElevation
        fields = ("job", "plan", "elevation","notes")

``` 

The admin shows the fields in the following order:

![image](https://github.com/django-import-export/django-import-export/assets/67477435/520e4092-0681-467c-9c03-af443237de13)

**To Reproduce**
Steps to reproduce the behavior:
1. Define a model resource with a custom `Field` with `attribute="<foreign_key_field_name>"`
2. Add the model to the admin via `ImportExportModelAdmin`
3. Navigate to the model in the admin site _"Import"_ button
4. Notice the `ForeignKey` based fields are at the end of the list

**Versions (please complete the following information):**
 - Django Import Export: 4.0.3
 - Python 3.10.6
 - Django 4.2.13

**Expected behavior**
The field order for the import view should be consistent with ordering defined via `fields` in the `Meta` class. Reverting back to 3.3.9 corrects the issue:
![image](https://github.com/django-import-export/django-import-export/assets/67477435/f551b32e-69ea-4631-a171-9379179847c1)


Discussion:
Thanks for raising, will take a look.


Does setting [`import_order`](https://django-import-export.readthedocs.io/en/latest/advanced_usage.html#field-ordering) solve the issue for you?
. 

Ah I re-read and see that you said it doesn't help.
I was looking through the changes made to v4 and believe it has to do with a change in the `Resource.get_fields` method here:

https://github.com/django-import-export/django-import-export/blob/6db364bf1943309ce90228b92fea2d0efd22522b/import_export/resources.py#L142-L146

Where previously, it was ordered according to the export fields:

https://github.com/django-import-export/django-import-export/blob/7683e92e5b0e2c53539669bc6dadb68b7f56adf3/import_export/resources.py#L357-L362



Resolving patch:
diff --git a/import_export/resources.py b/import_export/resources.py
--- a/import_export/resources.py
+++ b/import_export/resources.py
@@ -1085,7 +1085,7 @@ def get_export_headers(self, fields=None):
         return [force_str(field.column_name) for field in export_fields]
 
     def get_user_visible_fields(self):
-        return self.get_fields()
+        return self.get_import_fields()
 
     def iter_queryset(self, queryset):
         if not isinstance(queryset, QuerySet):


Test patch:
diff --git a/tests/core/tests/admin_integration/test_import.py b/tests/core/tests/admin_integration/test_import.py
--- a/tests/core/tests/admin_integration/test_import.py
+++ b/tests/core/tests/admin_integration/test_import.py
@@ -1007,3 +1007,45 @@ def test_import_preview_order(self):
             "</tr>"
         )
         self.assertRegex(str(response.content), target_row_re)
+
+
+class DefaultFieldsImportOrderTest(AdminTestMixin, TestCase):
+    """
+    Display correct import order based on default 'fields' declaration (issue 1845).
+    Ensure that the prompt text on the import page renders the
+    fields in the correct order.
+    """
+
+    def test_import_preview_order(self):
+        response = self.client.get(self.ebook_import_url)
+        # test display rendered in correct order
+        target_re = (
+            r"This importer will import the following fields:[\\n\s]+"
+            r"<code>id, author_email, name, published_date</code>[\\n\s]+"
+        )
+        self.assertRegex(str(response.content), target_re)
+
+
+class DeclaredImportOrderTest(AdminTestMixin, TestCase):
+    """
+    Display correct import order when 'import_order' is declared (issue 1845).
+    Ensure that the prompt text on the import page renders the
+    fields in the correct order.
+    """
+
+    def setUp(self):
+        super().setUp()
+        EBookResource._meta.import_order = ("id", "name", "published", "author_email")
+
+    def tearDown(self):
+        super().tearDown()
+        EBookResource._meta.import_order = ()
+
+    def test_import_preview_order(self):
+        response = self.client.get(self.ebook_import_url)
+        # test display rendered in correct order
+        target_re = (
+            r"This importer will import the following fields:[\\n\s]+"
+            r"<code>id, name, published_date, author_email</code>[\\n\s]+"
+        )
+        self.assertRegex(str(response.content), target_re)