0

If I write this order for AddressRegionDetailSerializer and AvailableAreaSerializer:

class AddressRegionDetailSerializer(ModelSerializer):
    """
    地域详情(包含可用区域)
    """
    availableareas = AvailableAreaSerializer(many=True, read_only=True)
    class Meta:
        model = AddressRegion
        fields = "__all__"

class AvailableAreaSerializer(ModelSerializer):
    """
    可用地区
    """

    class Meta:
        model = AvailableArea
        fields = "__all__"

There will report NameError issue:

NameError: name 'AvailableAreaSerializer' is not defined

in this line:

availableareas = AvailableAreaSerializer(many=True, read_only=True)    

So, I must put the AvailableAreaSerializer in the front. But however in my idea, I want to write the Serializer as the Models order, I don't want to break this law.

So, is there a simple way to maintain this order?

user7693832
  • 6,119
  • 19
  • 63
  • 114
  • If you want to keep things tidy, you could put your models in `models.py` and your serializers in `serializers.py` and then import your models into `serializers.py`. –  Nov 10 '17 at 07:30
  • @Blurp Yes, I do like this, I means, I want to the serializers order as same as the models order in .py files. – user7693832 Nov 10 '17 at 07:36

0 Answers0