OWASP / OWASP/Nest

refactor: replace manual get-then-save pattern with update_or_create in OWASP models

Open
#4,066 3 comments 0 reactions 0 assignees View on GitHub
bug enhancement
Dominant language
Python
Stars
451
Forks
707
Avg merge
22h 59m
Merged PRs (30d)
91

Description

## Describe the bug

Multiple OWASP models use a manual get-then-instantiate-then-save pattern that Django's built-in `update_or_create()` already handles. The current approach is verbose and reimplements existing Django functionality unnecessarily.

**Affected files:**
- `apps/owasp/models/entity_member.py` - `EntityMember.update_data()`
- `apps/owasp/models/chapter.py` - `Chapter.update_data()`

Current pattern in both files:

```python
try:
obj = Model.objects.get(**lookup)
except Model.DoesNotExist:
obj = Model(**lookup)
obj.from_dict(data)
if save:
obj.save()
```

## To Reproduce

Not a crash, this is a refactor suggestion.

## Expected behavior

Both methods should use `update_or_create()` for a cleaner, atomic, idiomatic Django approach:

```python
obj, _ = Model.objects.update_or_create(
**lookup_fields,
defaults={**update_fields}
)
```

## Are you going to work on fixing this?

- [x] Yes
- [ ] No

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.