Created
July 31, 2012 14:01
-
-
Save jsocol/3217262 to your computer and use it in GitHub Desktop.
Django Mass Assignment
This file contains hidden or 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
from django import forms | |
from myapp.models import Whatzit | |
class WhatzitForm(forms.ModelForm): | |
class Meta(object): | |
model = Whatzit | |
fields = ('foo', 'bar', 'baz') |
This file contains hidden or 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
from django.shortcuts import render | |
from myapp.models import Whatzit | |
def create_whatzit(request): | |
Whatzit.objects.create(**request.POST) | |
return render(request, 'created.html') | |
def update_whatzit(request, id): | |
whatzit = Whatzit.objects.filter(pk=id) | |
whatzit.update(**request.POST) | |
whatzit.save() | |
return render(request, 'saved.html') |
This file contains hidden or 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
from django import forms | |
from myapp.models import Whatzit | |
class WhatzitForm(forms.ModelForm): | |
class Meta(object): | |
model = Whatzit |
This file contains hidden or 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
from django.shortcuts import render | |
from myapp.forms import WhatzitForm | |
def create_whatzit(request): | |
form = WhatzitForm(form.POST or None) | |
if 'POST' == request.method and form.is_valid(): | |
w = form.save() | |
return render(request, 'created.html', {'whatzit': w}) | |
return render(request, 'create.html', {'form': form}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment