Django migrations with multiple databases

You have to run migrate once for each database, specifying the target with –database. Each time it will consult your router to see which migrations to actually perform on that database. I’m guessing it was designed this way to favor explicitness over implicitness. For example, your workflow might require you to migrate the different databases … Read more

How to pass url parameter to reverse_lazy in Django urls.py

I wouldn’t do this directly in the urls.py, I’d instead use the class-based RedirectView to calculate the view to redirect to: from django.views.generic.base import RedirectView from django.core.urlresolvers import reverse_lazy class RedirectSomewhere(RedirectView): def get_redirect_url(self, param): return reverse_lazy(‘resource-view’, kwargs={‘param’: param}, current_app=’myapp’) Then, in your urls.py you can do this: urlpatterns = patterns(”, url(r’^coolresource/(?P<param>\d+)/$’, RedirectSomewhere.as_view()), )

Get value of another field in Field level Validation in DRF

No that is not possible. If you need to access more than one value you have to use the Object-level validation (see docs): class Keys_Serializer(serializers.Serializer): key_id = serializers.IntegerField(required=True) key_name = serializers.CharField(required=True) value_id = serializers.IntegerField(required=False) def validate(self, data): # here you can access all values key_id = data[‘key_id’] value_id = data[‘value_id’] # perform you validation if … Read more

Add Serializer on Reverse Relationship – Django Rest Framework

Ahmed Hosny was correct in his answer. It required the many parameter to be set to True to work. So final version of the CartSerializer looked like this: class CartSerializer(serializers.ModelSerializer): cartitem_set = CartItemSerializer(read_only=True, many=True) # many=True is required class Meta: model = Cart depth = 1 fields = ( ‘id’, ‘date_created’, ‘voucher’, ‘carrier’, ‘currency’, ‘cartitem_set’, … Read more

Django: How do I override app-supplied urls in my project urlconf?

Apparently duplicate URLs are allowed in the urlconf, and the first match listed will have priority: urlpatterns = patterns(”, (r’^$’, include(‘glue.urls’)), (r’^foo/’, include(‘foo.urls’)), # This will override the same URL in bar’s urlconf: (r’^bar/stuff/$’, ‘glue.views.new_bar_stuff’, {‘arg’: ‘yarrgh’}, ‘bar_stuff’), (r’^bar/’, include(‘bar.urls’)), )

Django: “Reverse not found”

Do you have a view named viewPlan with which you do something like this in a template: {% url viewPlan %} or something like this in a view: reverse(‘viewPlan’) If you do that and you do not have a line that looks like this: url(r’^whatever/url/$’, ‘dev_env.profiles.views.viewPlan’, name=”viewPlan”), …in your url configuration I would imagine that’s … Read more

Django ListView – Form to filter and sort

You don’t need post. Pass the filter value and order_by in the url for example: …/update/list/?filter=filter-val&orderby=order-val and get the filter and orderby in the get_queryset like: class MyView(ListView): model = Update template_name = “updates/update.html” paginate_by = 10 def get_queryset(self): filter_val = self.request.GET.get(‘filter’, ‘give-default-value’) order = self.request.GET.get(‘orderby’, ‘give-default-value’) new_context = Update.objects.filter( state=filter_val, ).order_by(order) return new_context def … Read more

How to get logged in user’s uid from session in Django?

In case anyone wants to actually extract a user ID from an actual Session object (for whatever reason – I did!), here’s how: from django.contrib.sessions.models import Session from django.contrib.auth.models import User session_key = ‘8cae76c505f15432b48c8292a7dd0e54’ session = Session.objects.get(session_key=session_key) session_data = session.get_decoded() print session_data uid = session_data.get(‘_auth_user_id’) user = User.objects.get(id=uid) Credit should go to Scott Barnham

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)