in my django project i get a 'ModelFormOptions' object has no attribute 'concrete_model' error

五迷三道 提交于 2020-12-13 04:01:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!