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

14 update or create fix #15

Merged
merged 2 commits into from
Sep 23, 2024
Merged

14 update or create fix #15

merged 2 commits into from
Sep 23, 2024

Conversation

stefan-cardnell-rh
Copy link
Contributor

@stefan-cardnell-rh stefan-cardnell-rh commented Sep 23, 2024

For kafka_skip to work properly, We need to treat the following cases differently:

  1. When we create Model instances directly, either with or without PK specified. e.g. User.objects.create(id=1, kafka_skip=True or User.objects.create(kafka_skip=True).

    In this scenario we never need to set the kafka_skip value since the kafka_skip value was either set directly or it will default to False since the model field has default=False.

  1. When we retrieve an instance from DB, and save it again. E.g:

    instance = User.objects.create(id=1, kafka_skip=True)
    retrieved = User.objects.get(id=instance.id)
    retrieved.save()

    In this scenario, the DB loaded value (retrieved) has kafka_skip=True. Upon saving we want to reset kafka_skip to False only if kafka_skip was not set directly between get() and save().

Both scenarios are covered by adding a reset_kafka_skip variable and setting it correctly in _setattr_, __init__, and from_db.

Advantages:

  • This works for all ways of creating/retrieving instances, including QuerySet.create, QuerySet.get_or_create, QuerySet.update_or_create.
  • This works even when update_fields is not specified. Which is ideal as an open source package, since not everyone utilises update_fields.

@stefan-cardnell-rh stefan-cardnell-rh merged commit e666e8f into main Sep 23, 2024
12 checks passed
@stefan-cardnell-rh stefan-cardnell-rh deleted the 14-update-or-create-fix branch September 23, 2024 13:40
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