Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

Create custom Values method in django

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()
    
    
question from:https://stackoverflow.com/questions/65856575/create-custom-values-method-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...