How to perform filtering with a Django JSONField?

As per the Django JSONField docs, it explains that that the data structure matches python native format, with a slightly different approach when querying. If you know the structure of the JSON, you can also filter on keys as if they were related fields: object.filter(data__animal=”cat”) object.filter(data__name=”tom”) By array access: object.filter(data__0__animal=”cat”) Your contains example is almost … Read more

Django admin – inline inlines (or, three model editing at once)

You need to create a custom form and template for the LinkSectionInline. Something like this should work for the form: LinkFormset = forms.modelformset_factory(Link) class LinkSectionForm(forms.ModelForm): def __init__(self, **kwargs): super(LinkSectionForm, self).__init__(**kwargs) self.link_formset = LinkFormset(instance=self.instance, data=self.data or None, prefix=self.prefix) def is_valid(self): return (super(LinkSectionForm, self).is_valid() and self.link_formset.is_valid()) def save(self, commit=True): # Supporting commit=False is another can of worms. … Read more

context in nested serializers django rest framework

Ok i found a working solution. I replaced the ChildSerializer assignment in the Parent class with a SerializerMethodField which adds the context. This is then passed to the get_fields method in my CustomModelSerializer: class ChildSerializer(CustomModelSerializer): class Meta: fields = (‘c_name’, ) model = Child class ParentSerializer(CustomModelSerializer): child = serializers.SerializerMethodField(‘get_child_serializer’) class Meta: model = Parent fields … Read more

Django – [Errno 111] Connection refused

Looks like you are trying to send a mail (send_mail()) and your mail settings in your settings.py are not correct. You should check the docs for sending emails. For debugging purposes you could setup a local smtpserver with this command: python -m smtpd -n -c DebuggingServer localhost:1025 and adjust your mail settings accordingly: EMAIL_HOST = … Read more

update django database to reflect changes in existing models

As of Django 1.7+, built-in migrations support, allows for database schema migrations that preserve data. That’s probably a better approach than the solution below. Another option, not requiring additional apps, is to use the built in manage.py functions to export your data, clear the database and restore the exported data. The methods below will update … Read more

Django Rest framework, how to include ‘__all__’ fields and a related field in ModelSerializer ?

Like @DanEEStart said, DjangoRestFramework don’t have a simple way to extend the ‘all‘ value for fields, because the get_field_names methods seems to be designed to work that way. But fortunately you can override this method to allow a simple way to include all fields and relations without enumerate a tons of fields. I override this … Read more