Adrian Andreias

Jun 05, 2014

Django CreateUpdateView

Django is missing a generic class based view that handles both creating and updating a model object. As I was arguing in Ticket #22768 this is a common pattern that would save some code duplication and deserves a place in Django core. Until then here's an implementation:

class BaseCreateUpdateView(ModelFormMixin, ProcessFormView):
    """
    Base view for creating and updating an existing object.

    Using this base class requires subclassing to provide a response mixin.
    """

    object_url_kwargs = ['slug', 'pk']

    def get_object(self, queryset=None):
        if self.is_update_request():
            return super(BaseCreateUpdateView, self).get_object(queryset)
        else:
            return None

    def is_update_request(self):
        """
        Returns True if current request is an object update request, False if it's an object create request.

        Checks if the URL contains a parameter identifying an object.
        Possible URL parameter names are defined in self.object_url_kwargs
        """
        for object_kwarg in self.object_url_kwargs:
            if object_kwarg in self.kwargs:
                return True
        return False

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super(BaseCreateUpdateView, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super(BaseCreateUpdateView, self).post(request, *args, **kwargs)

Jan 22, 2013

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

since I now know that I'll get a meaningful message:

Problem installing fixture '.....json': 
......
File "....\lib\site-packages\django\core\serializers\json.py", line 47, in Deserializer
  raise DeserializationError(e)
DeserializationError: No JSON object could be decoded

Jan 21, 2013

Django model managed=True on testing

I have a bunch of Django models that have managed=False set, but I need Django to create these database tables while running unit testing.

I eventually found an article that describes a method to do this: Simplifying the Testing of Unmanaged Database Models in Django

Place the code in myproject/testrunner.py and specify in your settings.py or test_settings.py file:

TEST_RUNNER = 'myproject.testrunner.ManagedModelTestRunner'

And that's all. Django will now create the tables for the unmanaged models when running automatic unit testing.