我尝试使用Django REST框架创建一个端点,但当我尝试从端点获取数据时,我收到了以下错误:属性错误:"AgencyRegistration"对象没有属性"get"。以下是我的代码
models.py
`class AgencyRegistration(models.Model):
agency_name = models.CharField(max_length=200)
tax_id_number = models.IntegerField(max_length=12)
address_street = models.CharField(max_length=220)
city = models.CharField(max_length=50)
county = models.CharField(max_length=50)
state = models.CharField(max_length=100)
zip_code = models.CharField(max_length=50)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
title = models.CharField(max_length=200)
email = models.CharField(max_length=200)
phone = models.CharField(max_length=50)
children_amount = models.CharField(max_length=100)
age_range = models.CharField(max_length=100)
program_benefit = models.CharField(max_length=200)
organization_type = models.CharField(max_length=200)
how_did_you_hear = models.CharField(max_length=200)
def __str__(self):
return self.agency_name`
views.py
`def agency_registration(request):
agency_data = AgencyRegistration.objects.all()
serializer = AgencyRegistrationSerializer(agency_data, many=True)
return render(JsonResponse(serializer.data, safe=False))`
serializers.py
`class AgencyRegistrationSerializer(serializers.ModelSerializer):
class Meta:
model = AgencyRegistration
fields = [
'id',
'agency_name',
'tax_id_number',
'address_street',
'city',
'county',
'state',
'zip_code',
'first_name',
'last_name',
'title',
'email',
'phone',
'children_amount',
'age_range',
'program_benefit',
'organization_type',
'how_did_you_hear'
]`
urls.py
`urlpatterns = [
path('agency-registration/', views.AgencyRegistration),
]`
1条答案
按热度按时间von4xj4u1#
您不能将
Model
用作urlpatterns
中的path
。您只能在那里放置视图,例如您的agency_registration
。它应该是: