Skip to content

Instantly share code, notes, and snippets.

@georgedorn
Created September 25, 2011 18:59
Show Gist options
  • Save georgedorn/1240964 to your computer and use it in GitHub Desktop.
Save georgedorn/1240964 to your computer and use it in GitHub Desktop.
UpdateView that handles "create" as well
from myapp.models import MyModel, RelatedModel
from django.views.generic import UpdateView
class ModelCreateUpdateView(UpdateView):
model = MyModel
def get_object(self, *args, **kwargs):
related_id = self.kwargs.get('related_id')
related_model = RelatedModel.objects.get(pk=related_id)
my_object, _ = MyModel.objects.get_or_create(user=self.request.user,
related_model=related_model)
return my_object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment