Django REST Framework: adding additional field to ModelSerializer

I want to serialize a model, but want to include an additional field that requires doing some database lookups on the model instance to be serialized: class FooSerializer(serializers.ModelSerializer): my_field = … # result of some database queries on the input Foo object class Meta: model = Foo fields = (‘id’, ‘name’, ‘myfield’) What is the … Read more

Django fix Admin plural

How do I change some models name from “Categorys” to “Categories” on admin site in the new dev django version? In the old version (whithout admin sites and admin models) you could just do this; http://www.the-dig.com/blog/post/customize-plural-name-django-admin/ However – now setting verbose_name_plural inside my modeladmin based class does nothing. Anyone encouter the same issue? 2 Answers … Read more

How can I call a custom Django manage.py command directly from a test driver?

I want to write a unit test for a Django manage.py command that does a backend operation on a database table. How would I invoke the management command directly from code? I don’t want to execute the command on the Operating System’s shell from tests.py because I can’t use the test environment set up using … Read more

Create empty queryset by default in django form fields

I have this fields in form: city = forms.ModelChoiceField(label=”city”, queryset=MyCity.objects.all()) district = forms.ModelChoiceField(label=”district”, queryset=MyDistrict.objects.all()) area = forms.ModelChoiceField(label=”area”, queryset=MyArea.objects.all()) district comes from click on city and area comes from click on area. With queryset=MyDistrict.objects.all() and queryset=MyArea.objects.all() form will be very heavy. How can I make querysets empty by default? 2 Answers 2

Getting Django admin url for an object

Before Django 1.0 there was an easy way to get the admin url of an object, and I had written a small filter that I’d use like this: <a href=”https://stackoverflow.com/questions/694477/{{ object”admin_url }}” …. > … </a> Basically I was using the url reverse function with the view name being ‘django.contrib.admin.views.main.change_stage’ reverse( ‘django.contrib.admin.views.main.change_stage’, args=[app_label, model_name, object_id] … Read more

In a django model custom save() method, how should you identify a new object?

I want to trigger a special action in the save() method of a Django model object when I’m saving a new record (not updating an existing record.) Is the check for (self.id != None) necessary and sufficient to guarantee the self record is new and not being updated? Any special cases this might overlook? 13 … Read more