Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Last active March 20, 2019 06:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeinthehole/6178034 to your computer and use it in GitHub Desktop.
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
# 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)
@codeinthehole
Copy link
Author

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 and user to pass to the constructor.

@AndrewIngram
Copy link

Assuming you're using factories, I'm not sure it's much of a problem to generate a review and a user.

@AndrewIngram
Copy link

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