[Fixed]-How to get all objects by instance in django


You can use __class__ to get the model name and then get all the objects.

In [1]: my_instance.__class__
Out[1]: app_name.models.SometingModel # returns the model 

In[2]: my_instance.__class__.objects.all()
Out[2]: [..list of objects..] # returns queryset

Another option is to use _meta.model which @Spectras also mentioned above.

In[3]: my_instance._meta.model
Out[3]: app_name.models.SometingModel # returns the model

In[4]: my_instance._meta.model.objects.all()
Out[4]: [..list of objects..] # returns queryset

Then in your models, you can do something like:

class SometingModel(model.Model):

    def get_all_objects(self):
        queryset = self._meta.model.objects.all()
        # can use the below method also
        # queryset = self.__class__.objects.all()   
        return queryset

Leave a comment