Created
September 25, 2011 18:59
-
-
Save georgedorn/1240964 to your computer and use it in GitHub Desktop.
UpdateView that handles "create" as well
This file contains 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 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