本文介绍了在我的Django项目中,我得到一个'ModelFormOptions'对象没有属性'concrete_model'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为django rest项目添加了一个表格.但是,我不断收到"ModelFormOptions"对象没有属性"concrete_model"错误.

I have added a form for my django rest project. However, I keep getting a 'ModelFormOptions' object has no attribute 'concrete_model' error.

在我的User类中,我添加了一个新字段"password",并且我试图创建一个表单.然后调用序列化程序.

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.

我的模型:

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')

表格

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('email', 'password')

序列化器

class UserSerializer(serializers.ModelSerializer):
class Meta:
    model = UserForm
    fields = '__all__'

这是发生的错误:

AttributeError'ModelFormOptions'对象没有属性'concrete_model'

AttributeError at /users/'ModelFormOptions' object has no attribute 'concrete_model'

推荐答案

您的 UserSerializer 需要引用您的模型,而不是表单:

Your UserSerializer needs to the refer to your model, not the form:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

这篇关于在我的Django项目中,我得到一个'ModelFormOptions'对象没有属性'concrete_model'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:17