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 postgresql json field schema validation

I wrote a custom validator using jsonschema in order to do this. project/validators.py import django from django.core.validators import BaseValidator import jsonschema class JSONSchemaValidator(BaseValidator): def compare(self, value, schema): try: jsonschema.validate(value, schema) except jsonschema.exceptions.ValidationError: raise django.core.exceptions.ValidationError( ‘%(value)s failed JSON schema check’, params={‘value’: value} ) project/app/models.py from django.db import models from project.validators import JSONSchemaValidator MY_JSON_FIELD_SCHEMA = { ‘schema’: … Read more

How can I access the form submit button value in Django?

Submit is an HTML Form structure… You must use name attribute of form objects as follows… In your template: <form> … <input type=”submit” name=”list” value=”List Objects” /> </form> <form> … <input type=”submit” name=”do-something-else” value=”Do Something Else” /> </form> In your view: if ‘list’ in request.POST: # do some listing… elif ‘do-something-else’ in request.POST: # do … Read more

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

How to show related objects in Django/Admin?

You can use “Inlines” to visualize and edit Things of a certain Category in the admin detail for that category: In the admin.py file, create an Inline object for Thing (ThingInline) and modify your CategoryAdmin class to have an inline of type ThingInline like this: … class ThingInline(admin.TabularInline): model = Thing class CategoryAdmin(admin.ModelAdmin): inlines = … 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

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