Skip to content
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

feat(relations): save with update_fields #765

Merged
merged 1 commit into from
Apr 10, 2024
Merged

Conversation

koeaw
Copy link
Contributor

@koeaw koeaw commented Apr 8, 2024

When Property's save method is called with
update_fields to update any of the name fields,
make sure both name fields are considered when
saving.

@koeaw koeaw marked this pull request as ready for review April 8, 2024 07:26
@koeaw
Copy link
Contributor Author

koeaw commented Apr 8, 2024

I'd consider this a fix but it doesn't fix existing code but adds something new, so... unsure which commit type to use.

@b1rger
Copy link
Contributor

b1rger commented Apr 9, 2024

I'd consider this a fix but it doesn't fix existing code but adds something new, so... unsure which commit type to use.

I'd use fix - simply because feat leads to a version bump.

I think the snippet is a bit hard to read - I understand the faszination with the walrus operator, but in this case it makes it hard to follow the logic (at least for me). Wouldn't something like this be more readable?:

name_fields = {"name_forward", "name_reverse"}
update_fields = kwargs.get("update_fields", [])
if name_fields.intersection(update_fields):
    kwargs["update_fields"] = set(update_fields) | include_fields

(But maybe thats missing a corner case, the original code works for ...)

@koeaw
Copy link
Contributor Author

koeaw commented Apr 9, 2024

I'd use fix - simply because feat leads to a version bump.

Ah, I wasn't aware (or possibly had forgotten), good to know. Will change it.

I think the snippet is a bit hard to read - I understand the faszination with the walrus operator

Guilty as charged. XD I'll simplify it.

When Property's save method is called with
update_fields to update name_forward, make sure
name_reverse is saved as well.
@koeaw koeaw force-pushed the kk/fix/property_update_fields branch from 66e11d4 to a9e52c5 Compare April 9, 2024 16:18
@koeaw
Copy link
Contributor Author

koeaw commented Apr 9, 2024

I realised that with deprecated_name having got dropped, the only remaining name field that's dependent on another is name_reverse, so set comparison via intersection has become superfluous.

I kept one walrus operator in this adaptation because I think it is easy enough to read/understand the way it's used here; the "long form" version I would have gone with resulted in forced reformatting of the if statement, which I didn't think helped readability either.

@b1rger
Copy link
Contributor

b1rger commented Apr 10, 2024

Oke, but now we could simply not use a Set at all, right?

 if (update_fields := kwargs.get("update_fields")) is not None:
     if "name_forward" in update_fields and "name_reverse" not in update_fields:
         kwargs["update_fields"].append("name_reverse")

@koeaw
Copy link
Contributor Author

koeaw commented Apr 10, 2024

Oke, but now we could simply not use a Set at all, right?

update_fields can be set to any iterable which can hold strings, so converting to set is the most universal approach, I believe. (Inclulding if it were set to just a string, I think?)

Docs

@b1rger
Copy link
Contributor

b1rger commented Apr 10, 2024

update_fields can be set to any iterable which can hold strings, so converting to set is the most universal approach, I believe. (Inclulding if it were set to just a string, I think?)

ah, oke!

@b1rger b1rger merged commit c4fe0e3 into main Apr 10, 2024
11 checks passed
@koeaw koeaw deleted the kk/fix/property_update_fields branch September 30, 2024 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants