问题
I have added a form for my django rest project. However, I keep getting a 'ModelFormOptions' object has no attribute 'concrete_model' error.
In my User class I have added a new field 'password' and I was trying to create a form. and call the serializer upon it.
My Model:
class User(models.Model):
gender = models.CharField(max_length=10, blank=False, choices=GENDER)
first_name = models.CharField(max_length=20, blank=False)
last_name = models.CharField(max_length=20, blank=False)
position = models.CharField(max_length=50, blank=True)
birthday = models.DateField(auto_created=False, blank=False)
email = models.EmailField(max_length=50)
phone = models.CharField(max_length=15, blank=False)
password = models.CharField(max_length=100, default='something')
Form
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ('email', 'password')
Serializer
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = UserForm
fields = '__all__'
Here is the Error that occures:
AttributeError at /users/ 'ModelFormOptions' object has no attribute 'concrete_model'
回答1:
Your UserSerializer
needs to the refer to your model, not the form:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
来源:https://stackoverflow.com/questions/54632816/in-my-django-project-i-get-a-modelformoptions-object-has-no-attribute-concret