1

I've just plugged this old JSONField model snippet into my django application. It looks like it's working, but throws this warning whenever the server revalidates:

$ sudo python manage.py runserver
Validating models...

/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/models/fields/subclassing.py:80:
DeprecationWarning: A Field class whose get_db_prep_save method hasn't been updated to take a `connection` argument.
  new_class = super(SubfieldBase, cls).__new__(cls, name, bases, attrs)
0 errors found

What does this mean? How do I fix it?

3 Answers 3

1

It's a warning telling you that the the custom JSON field implemented in that snippet hasn't included the connection argument that has been introduced in django 1.2 due to multiple database support being implemented.

With regards the method itself: If you are writing a custom model field, you can use the get_db_prep_save to convert the python object you are working with (in this case a JSON object) into a form that the database backend can manage (in this case a string) before it is saved to the DB. Here are the release notes mentioning it

With regard the connection argument, it refers to the current database (at the time of execution - to get the default you can call django.db.connection) and it is included to ensure that the correct database is provided when calling that method so that any custom backend logic or convertions can be carried out before the value is saved to the db. You can read more about connections and cursors here

Sign up to request clarification or add additional context in comments.

3 Comments

Gotcha -- thanks. Can you point me to a example or reference for writing custom model fields? The releases notes say how things changed in Django 1.2, but don't give much help for those of us who didn't understand the syntax to begin with...
Silly me: the first google result for "django custom model field" nails it: docs.djangoproject.com/en/dev/howto/custom-model-fields
Custom model fields are one of the less documented aspects of django although there is an official reference page.. You just need to be careful not to confuse model field with form field or widgets. Here and here are two tutorials. Half the battle is knowing when you need to write your own model fields - for that, I haven't an answer
0

You can also use http://pypi.python.org/pypi/django-jsonfield, it is basically a packaging-up of the code snippet you mention.

(I had an older version that gave me the same connection error you mentioned; the newer versions have fixed this).

Comments

0

You can also try this one: https://github.com/vialink/vlk-django-jsonfield

We are using it in some projects and it is working fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.