python - Function for every a django-model -


i have django-models , made function, example:

def function(django-model):     return django-model.objects.order_by('-date') 

i want execute function models, don't know how works.

function(django-model-1()) 

or

function(django-model-2) 

return:

attributeerror: manager isn't accessible via django-model-1 instances 

thanks , sorry english.

upd:

sorry, maybe poorly explained.

i need universal function manipulating models.

from django-model-1.models import django-model-1 django-model-2.models import django-model-2      def function(django-model):         return django-model.objects.order_by('-date') # example  function(django-model-1) function(django-model-2) 

but it's don't work.

if make without function, it's work.

django-model-1.objects.order_by('-date') 

django model definitions define tables store records , define instance of object looks , how behaves. objects special attribute on model class (not on instances) allow things all of instances (such query tables). objects type of modelmanager.

what want define custom manager.

let's have model definition so:

class somemodel(models.model):      pass 

well, can define custom manager model custom method so:

class somemodelmanager(models.manager):     def datesort(self):         return self.order_by('-date') 

now, can attach manager model definition , give whatever name want:

class somemodel(models.model):     sorting = somemodelmanager() 

now, can import , use sorting manager:

 >>> some_app.models import somemodel  >>> somemodel.sorting.date()  # not called on instance!  [sorted results] 

the key thing realize not want work on instances of somemodel, actual modelmanager itself. confusing in django because them interacting same class.

see docs more info:

https://docs.djangoproject.com/en/1.9/topics/db/managers/


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -