class ArticleForm(forms.ModelForm):class Meta:model = Articlefields = ['url',]
The Article model has a url field which is unique. I want users to enter a URL. If the Article already exists, it gets reused. Otherwise, a new instance is created.
And the corresponding view:
1 @login_required2 def get_article(request):3 if request.method == 'POST':
4 # Should I get a new instance here?5 form = ArticleForm(request.POST, instance=None)6 if form.is_valid():7 article, created = Article.objects.get_or_create(url=form.cleaned_data['url'])
9 # Some more work goes here10 return redirect(reverse('articles:list'))11 else:12 form = ArticleForm()13 return render(request, 'articles/get_article.html', {'form': form})
The form is validating the uniqueness of the model, which is great. The dilemma I am having is that I would like to bind the form to the existing instance in line 4. But at that point, the form has not been cleaned, so I wouldn't want to hit the database with it.
What's the proper pattern to do this?
Thanks a lot!if request.method == 'POST':
try:
article = Article.objects.get(url=form.cleaned_data['url'])
except Article.DoesNotExist:
article = None
form = ArticleForm(request.POST, instance=article)
if form.is_valid():
form.save()
return redirect(reverse('articles:list'))
else:
form = ArticleForm()
return render(request, 'articles/get_article.html', {'form': form})