Django: Get list of model fields?

Django versions 1.8 and later: You should use get_fields(): [f.name for f in MyModel._meta.get_fields()] The get_all_field_names() method is deprecated starting from Django 1.8 and will be removed in 1.10. The documentation page linked above provides a fully backwards-compatible implementation of get_all_field_names(), but for most purposes the previous example should work just fine. Django versions before … Read more

Django – limiting query results

Django querysets are lazy. That means a query will hit the database only when you specifically ask for the result. So until you print or actually use the result of a query you can filter further with no database access. As you can see below your code only executes one sql query to fetch only … Read more

Dynamically adding a form to a Django formset

This is how I do it, using jQuery: My template: <h3>My Services</h3> {{ serviceFormset.management_form }} {% for form in serviceFormset.forms %} <div class=”table”> <table class=”no_error”> {{ form.as_table }} </table> </div> {% endfor %} <input type=”button” value=”Add More” id=”add_more”> <script> $(‘#add_more’).click(function() { cloneMore(‘div.table:last’, ‘service’); }); </script> In a javascript file: function cloneMore(selector, type) { var newElement … Read more

Django rest framework, use different serializers in the same ModelViewSet

Override your get_serializer_class method. This method is used in your model mixins to retrieve the proper Serializer class. Note that there is also a get_serializer method which returns an instance of the correct Serializer class DualSerializerViewSet(viewsets.ModelViewSet): def get_serializer_class(self): if self.action == ‘list’: return serializers.ListaGruppi if self.action == ‘retrieve’: return serializers.DettaglioGruppi return serializers.Default # I dont’ … Read more

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