Create custom Values method in django

Viewed 286

I want to create a method semi to values method in Django QuerySet.

The values method problems are:

  1. Miss order of fields in querySet if I make myquery = MyModel.objects.values('field1','field2','field3') when I print querSet it give me [{'field2':'data','field1':'data','field3':'data'},...]. so this miss order will cause a problem at Union of queryset.
  2. if the field is choices at model then values(...) will give me the key of dictionary instead of its value.
  • I want to create my custom values method using django Manager

    
    class MyModelManager(models.Manager):
        def values(self, *fields):
            # I want to get model belong to this manger
            # then I want to check the fields if its in models (we have fields came from annotate)
            # after that I want to exclude to the fields that has choices 
            # next I want to call the super values method and passed to it the values that does not have choices
            # finally I want to reorder query according to passed fields
    
    class MyModel(models.model):
        # An example model
        objects = MyModelManager()
    
    
0 Answers
Related