1
我想遍歷ModelSerializer中的字段並希望使這些字段成爲必需字段。這不起作用。我怎樣才能做到這一點。有人請幫助我。django rest framwork在模型序列化程序中迭代通過字段
class CustomerSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = ("email", "phone_no", "full_name", "landline_no")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields:
self.fields[field].required = True
self.fields[field].allow_blank = False
您是否試過'超級(CustomerSerializer,self).__ init __(* args,** kwargs)'? – zaidfazil
@zaidfazil它是蟒蛇3.它會工作 – tstudent
調用'super()'就像這是推薦的方式。試試吧!我已經在我的機器上嘗試過了,它工作正常。另外,你的意思是「不工作」,有沒有錯誤? – zaidfazil