Binding a ModelForm to an existing instance

110 views
Skip to first unread message

Carlos Mermingas

unread,
Sep 4, 2016, 10:36:29 PM9/4/16
to Django users
I have a ModelForm:

class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = ['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_required
 2 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 here
10            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!

Vadim Serdiuk

unread,
Sep 5, 2016, 9:35:27 AM9/5/16
to Django users
Hi Carlos. 
At first you should get an article, then put it to the form via instance parameter. If article is not found, instance is None. Than if form is valid, method 'save' creates new object or updates old one.

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})


понедельник, 5 сентября 2016 г., 5:36:29 UTC+3 пользователь Carlos Mermingas написал:
Reply all
Reply to author
Forward
0 new messages