Last active
March 20, 2019 06:13
-
-
Save codeinthehole/6178034 to your computer and use it in GitHub Desktop.
Comparison of three ways of assigning excluded fields to a model when creating via a form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Question - how best to handle model fields that are excluded from a form's | |
# fields. | |
# ------------------------------------------------------------ | |
# Option 1: assign fields to instance before passing into form | |
# ------------------------------------------------------------ | |
# views.py | |
instance = Vote(review=review, user=request.user) | |
form = VoteForm(data=request.POST, instance=instance) | |
# -------------------------------------------------------- | |
# Option 2: pass excluded field values to form constructor | |
# -------------------------------------------------------- | |
# views.py | |
form = VoteForm(data=request.POST, review=review, user=request.user, | |
instance=instance) | |
# forms.py | |
class VoteForm(forms.ModelForm): | |
def __init__(self, review, user, *args, **kwargs): | |
super(VoteForm, self).__init__(*args, **kwargs) | |
self.instance.review = review | |
self.instance.user = user | |
# ----------------------------------------------------------------------------- | |
# Option 3: pass excluded field values to form constructor but defer assignment | |
# ----------------------------------------------------------------------------- | |
# views.py | |
form = VoteForm(data=request.POST, review=review, user=request.user, | |
instance=instance) | |
# forms.py | |
class VoteForm(forms.ModelForm): | |
def __init__(self, review, user, *args, **kwargs): | |
self.review = review | |
self.user = user | |
super(VoteForm, self).__init__(*args, **kwargs) | |
def save(self, commit=True): | |
vote = super(VoteForm, self).save(commit=False) | |
vote.review = self.review | |
vote.user = self.user | |
if commit: | |
vote.save() | |
return vote | |
# (This method is bad as the model's clean method doesn't have access to the | |
# review and user instances). | |
# Option 1 is best right? (Confession - I've been using option 2 for ages) |
Assuming you're using factories, I'm not sure it's much of a problem to generate a review and a user.
Alternatively, just pass in None if your test doesn't hit the part of the code where review and user are actually required to be anything in particular. And if it does hit those parts, you're gonna need to construct proper instances anyway.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the record, I think option 2 is better for Oscar as the API is simpler (as @AndrewIngram pointed out). It's more cryptic when you have to assign things to the
instance
argument.However, option 1 is slightly easier to unit test as you don't have to create instances of a
review
anduser
to pass to the constructor.