method to turn on auto_add and auto_add_now fields #111
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey guys,
I had a database
model
where I hadcreated
andmodified
DateTime
fields withauto_now
andauto_now_add=True
. In one of my test case I had useddjango_seed
to generate the objects. And I had another test case where I was testing API with post request to create the object. The issue I was facing was when we runseeder.execute()
seeder sets theauto_now
andauto_now_add
fields ofmodel
toFalse
and as a result, I was gettingNOT NULL constraint failed:
error inAPI
oncreated
andmodified
field because seeder had set them asauto_now=False
andauto_now_add=False
.So, I had added a method
turn_on_auto_add_fields(model)
which will setauto_now
andauto_now_add=True
. So, that it should be added automatically through the API call.I also have added TestCases to raise the error and fix the error after turning on the auto fields.
And a lot of stuff had changed maybe because of auto-formatting. Let me know if I need to make it like it was before :).
Fixes #109