Pass extra arguments to Serializer Class in Django Rest Framework

It’s very easy with “context” arg for “ModelSerializer” constructor. For example: in view: my_objects = MyModelSerializer( input_collection, many=True, context={‘user_id’: request.user.id} ).data in serializers: class MyModelSerializer(serializers.ModelSerializer): … is_my_object = serializers.SerializerMethodField(‘_is_my_find’) … def _is_my_find(self, obj): user_id = self.context.get(“user_id”) if user_id: return user_id in obj.my_objects.values_list(“user_id”, flat=True) return False … so you can use “self.context” for getting extra params. … Read more

Django: Redirect logged in users from login page

I’m assuming you’re currently using the built-in login view, with (r’^accounts/login/$’, ‘django.contrib.auth.views.login’), or something similar in your urls. You can write your own login view that wraps the default one. It will check if the user is already logged in (through is_authenticated attribute official documentation) and redirect if he is, and use the default view … Read more

Django, Retrieve IP location

GeoDjango looks like it will suit your needs. I’m not sure exactly how you would want to direct users, but using the GeoIP API, you can do something like: from django.contrib.gis.utils import GeoIP g = GeoIP() ip = request.META.get(‘REMOTE_ADDR’, None) if ip: city = g.city(ip)[‘city’] else: city = ‘Rome’ # default city # proceed with … Read more

Django: Can class-based views accept two forms at a time?

Here’s a scaleable solution. My starting point was this gist, https://gist.github.com/michelts/1029336 i’ve enhanced that solution so that multiple forms can be displayed, but either all or an individual can be submitted https://gist.github.com/jamesbrobb/748c47f46b9bd224b07f and this is an example usage class SignupLoginView(MultiFormsView): template_name=”public/my_login_signup_template.html” form_classes = {‘login’: LoginForm, ‘signup’: SignupForm} success_url=”my/success/url” def get_login_initial(self): return {’email’:’[email protected]’} def get_signup_initial(self): return … Read more

How to write setup.py to include a Git repository as a dependency

After digging through the pip issue 3939 linked by @muon in the comments above and then the PEP-508 specification, I found success getting my private repo dependency to install via setup.py using this specification pattern in install_requires (no more dependency_links): install_requires = [ ‘some-pkg @ git+ssh://[email protected]/someorgname/[email protected]#egg=some-pkg’, ] The @v1.1 indicates the release tag created on … Read more

Django: accessing session variables from within a template?

You need to add django.template.context_processors.request to your template context processors. Then you can access them like this: {{ request.session.name }} In case you are using custom views make sure you are passing a RequestContext instance. Example taken from documentation: from django.shortcuts import render_to_response from django.template import RequestContext def some_view(request): # … return render_to_response(‘my_template.html’, my_data_dictionary, context_instance=RequestContext(request)) … Read more