New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Declaring a Resource Field changes the export order #1663
Comments
Hello, I looked into the issue and it seems to be caused by Declared Fields + Model Fields. My suggested solution is re construct new Current behavior# Resource class already has declared fields so model fields are appended
new_class.fields.update(OrderedDict(field_list)) Suggested change# Model fields are first then declared fields
new_class.fields = OrderedDict([
*field_list,
*new_class.fields.items()
]) Exampleclass BookResource(ModelResource):
publish_date = fields.Field(
attribute="publish_date",
column_name="publish_date",
widget=widgets.DateWidget("%d.%M.%Y")
)
class Meta:
model = Book
fields = ["id", "name", "publish_date"] |
Thanks for looking into it - it sounds like a decent solution. |
I was going to create a PR but should this be the intended behavior? Basically, declared fields are going to be in the end. This is not going to preserve the actual model field order. Exampleclass Book(Model):
name = CharField()
published = DateField()
author = CharField()
author_email = EmailField()
class BookResrouce(ModelResource):
published = fields.Field(
attribute="published",
column_name="published_date",
widgets.DateWidget("%d.%M.%Y")
)
class Meta:
model = Book
# My solution's outcome
# id, name, author, author_email, published
# Or should it be
# id, name, published, author, author_email |
Do you know if it would be possible to do this, because that would be the best outcome. |
Describe the bug
If declaring a Field in a Resource, this causes the export order to change:
Without declaration:
With declaration:
To Reproduce
Declare a Field in Resource:
Export the book.
Note this doesn't occur (i.e. order is preserved) if you use the widgets dict:
Versions (please complete the following information):
Expected behavior
Declaring Field should not affect the export order.
The text was updated successfully, but these errors were encountered: