[Answer]-Custom Django Model that Returns Data to View

1👍

This whole approach makes no sense at all.

As you have already been told elsewhere, subclassing models.Field is utterly inappropriate. A field is part of a model, not a standalone thing, and doesn’t make SQL queries by itself. If you really need to do it this way, subclass Model.

But I don’t understand why you want to make any kind of class at all. Why not use the Django ORM? Or if you insist on doing it via raw SQL, why not make getVioPoints a simple standalone function that you can call, which returns the data? Since there’s no state being stored – your class queries the db on instantiation and returns the data directly – there’s no point in having a class at all.

(And your error is happening because you need to start the shell with manage.py shell, and then import your file. But it still won’t work, you’ll just get a different error.)

Leave a comment